1.Fragment是什么?
碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛。虽然碎片对你来说应该是个全新的概念,但我相信你学习起来应该毫不费力,因为它和活动实在是太像了,同样都能包含布局,同样都有自己的生命周期。你甚至可以将碎片理解成一个迷你型的活动,虽然这个迷你型的活动有可能和普通的活动是一样大的。
2.Fragment的生命周期
Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。如下图,这是fragment与activity的生命周期关系:

这里具体的方法不再赘述
3.使用Fragment进行窗口切换
一共有4个界面,由每个Activity对应一个Fragment,通过FragmentTransaction互相切换。
Fragment代码:
package nju.edu.futures_android.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import nju.edu.futures_android.R;
import nju.edu.futures_android.Util.DividerItemDecoration;
import nju.edu.futures_android.Util.HomeAdapter;
import nju.edu.futures_android.Util.pagerViewAdapter;
import nju.edu.futures_android.VO.URLVO;
import nju.edu.futures_android.logic.GetURL;
import nju.edu.futures_android.logic.OnItemClickListener;
/**
* Created by nick on 16/8/13.
*/
public class Fragment_home extends Fragment implements OnItemClickListener {
private RollPagerView pagerView;
private RecyclerView mRecyclerView;
private HomeAdapter mAdapter;
private GetURL connector ;
private Vector<URLVO> urls;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab_home, container, false);
getNews();
initView(view);
return view;
}
private void getNews() {
connector = new GetURL();
urls=connector.getURL();
}
public void initView(View parent){
//滚动条
pagerView = (RollPagerView) parent.findViewById(R.id.roll_view_pager);
pagerView.setPlayDelay(3000); //设置播放时间间隔
pagerView.setAnimationDurtion(1000);//设置动画时长
pagerView.setAdapter(new pagerViewAdapter());//设置适配器
pagerView.setHintView(new ColorPointHintView(parent.getContext(),Color.YELLOW, Color.WHITE));
//新闻list
mRecyclerView = (RecyclerView)parent.findViewById(R.id.id_list_news);
mRecyclerView.setLayoutManager(new LinearLayoutManager(parent.getContext()));
mAdapter = new HomeAdapter(urls);
mAdapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addItemDecoration(new DividerItemDecoration(parent.getContext(),
DividerItemDecoration.VERTICAL_LIST));
}
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent();
intent.putExtra("url",urls.get(position).getUrl());
intent.setAction("nju.edu.futures.news");
startActivity(intent);
}
}
Activity代码:
4.这样做的优势
-
FragmentManager提供了对Activity运行时的Fragment的添加、删除、替换的操作。在Activity运行期间可以添加Fragment而不是在XML布局文件中进行定义。如果打算在Activity中改变Fragment的生命过程。如果要执行添加、删除、修改的操作,必须通过FragmentManager的对象获得一个FragmentTransaction对象,通过它的API来执行这些操作。
-
将页面切换的代码集中在Fragment,使得Activity的类中不必管理界面切换,降低代码的耦合度,使程序更易维护。
-
使用了Fragment以适配不同分辨率的硬件。使得应用能够适配不同的布局。
-
对于类似Tab页面切换,如果使用Activity则需要一次性全部加载,而Fragment则不需要,降低了初始化所需要的加载时间。

645

被折叠的 条评论
为什么被折叠?



