因为项目中经常用到tabLayout + viewpager 结合使用,页面切换时要用到延迟加载的情况,所以单独写出一个demo,方便查看和便于使用
1.首先新建一个BaseLazyFragment
/**
* Created by xuqianqian on 2018/2/28.
*/
public abstract class BaseLazyFragment extends Fragment {
protected View mRootView;
public Context mContext;
protected boolean isVisible;
private boolean isPrepared;
private boolean isLoad = false;
//最先执行
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getUserVisibleHint()) {//当前显示
isVisible = true;
if(!isLoad){
lazyLoad();
}
} else {
isVisible = false;
//onInvisible();
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mRootView == null) {
mRootView = getLayout();
}
return mRootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
isPrepared = true;
//如果已经加载过了,就无需再重新加载
if(!isLoad){
lazyLoad();
}
}
/**
* 懒加载
*/
protected void lazyLoad() {
if (!isPrepared || !isVisible) {
return;
}
isLoad = true;
initData();
}
// protected void onInvisible() {
//
// }
//初始化布局View
public abstract View getLayout();
public abstract void initData();
}
2.让你的fragment extend BaseLazyFragment
public class GankFragment extends BaseLazyFragment {
String type;
public static GankFragment newInstance(String type) {
GankFragment gankFragment = new GankFragment();
Bundle bundle = new Bundle();
bundle.putString("type", type);
gankFragment.setArguments(bundle);
return gankFragment;
}
//加载布局
@Override
public View getLayout() {
type = getArguments().getString("type");
TextView tv = new TextView(getActivity());
tv.setText(type);
tv.setGravity(Gravity.CENTER);
return tv;
}
@Override
public void initData() {
//初始化一些控件,加载网络数据
}
}
3.剩下的就是FragmentPagerAdapter了
public class TitleAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
private List<String> types;
public TitleAdapter(FragmentManager fm, List<Fragment> fragments, List<String> types) {
super(fm);
this.fragments = fragments;
this.types = types;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return types.get(position);
}
}
MainActivity.java
private List<String> titles = new ArrayList<>();
private List<Fragment> fragments = new ArrayList<>();
TitleAdapter titleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tablayout = findViewById(R.id.tablayout);
ViewPager viewpager = findViewById(R.id.viewpager);
titles.add("android");
titles.add("ios");
titles.add("web");
titles.add("java");
fragments.add(GankFragment.newInstance("android"));
fragments.add(GankFragment.newInstance("ios"));
fragments.add(GankFragment.newInstance("web"));
fragments.add(GankFragment.newInstance("java"));
titleAdapter = new TitleAdapter(getSupportFragmentManager(), fragments, titles);
viewpager.setAdapter(titleAdapter);
tablayout.setTabMode(TabLayout.MODE_FIXED);
tablayout.setupWithViewPager(viewpager);
}