ViewPager的刷新、限制预加载、缓存所有

【框架】:

ViewPager的刷新、限制预加载、缓存所有

公共部分:左侧菜单、TitleBar、RadioGroup(3个RadioButton:X、Y、Z)

选择X页面:指示器+ViewPager

【要达成的效果】:

(1)左侧选择A,进入X页面,X1联网刷新页面,此时禁止X2预加载—>滑动到X2页面,X2才联网刷新—>X3—>X4;

(2)从X4再滑到X3、X2、X1时,X1、X2、X3不需要再次刷新,假设停留在X2页面;

(3)左侧再点选择B,或者先点Y再点选择B再点X,这时候X2联网刷新;

(4)X2页面滑到X1、X3、X4页面,X1、X3、X4重新刷新一次并缓存。

【需要解决的几个问题及说明】:

需要解决:

(1)ViewPager的Fragment刷新问题

(2)禁止ViewPager的预加载问题

(3)加载后即缓存,除非再次联网请求

说明:

因为抽取了BaseFragment、联网加载页面LoadingPage等,代码结构有点复杂,这里只摘取跟本文相关的代码

【1】刷新问题

initData初始化:fManager=getChildFragmentManager();adapter = new MyViewPagerAdapter(fManager,fenleiFragments);vpSearch.setAdapter(adapter);vpSearch.setOffscreenPageLimit(fenleiFragments.size()-1);//设置缓存所有

FragmentPagerAdapter: class MyViewPagerAdapter extends FragmentPagerAdapter {

private ArrayList<Fragment> fragments; private FragmentManager manager; private int mChildCount=0;

public MyViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fenleiFragments) { super(fm); this.manager=fm; this.fragments=fenleiFragments; }

@Override public void notifyDataSetChanged() { mChildCount=getCount(); super.notifyDataSetChanged(); }

@Override public Fragment getItem(int position) { return fenleiFragments.get(position); }

@Override public int getCount() { return fenleiFragments == null ? 0 : fenleiFragments.size(); }

@Override public CharSequence getPageTitle(int position) { return fenlei_names.get(position); }

@Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); }

@Override public int getItemPosition(Object object) { //方式1: if(mChildCount>0){ mChildCount--; return POSITION_NONE; } return super.getItemPosition(object);

//方式2: //return POSITION_NONE; /*备注:方式1和方式2在只有3个Fragment的情况下,效果一样,多于3个的情况没有验证*/ }

}

 收到广播刷新

左侧菜单更换选择,刷新菜单更换选择、等等,这里用广播的方式通知刷新

//注册为广播接收者 private LocalBroadcastManager lbm; private BroadcastReceiver selectChangedReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //更新页面(适配器) /*----------------------------------------------------------------*/ if(vpSearch.getAdapter()!=null){ FragmentManager cfm = getChildFragmentManager(); FragmentTransaction ft = cfm.beginTransaction(); List<Fragment> fragments = cfm.getFragments(); if(fragments!=null && fragments.size()>0){ for(int i = 0; i < fragments.size(); i++) { ft.remove(fragments.get(i)); } } ft.commit(); }

fenleiFragments.clear(); fenleiFragments.add(new CategoryFragment(Urls.getFenleiUrl(0))); fenleiFragments.add(new BrandFragment(Urls.getFenleiUrl(1))); fenleiFragments.add(new DongtaiFragment(Urls.getFenleiUrl(2)));

adapter.notifyDataSetChanged(); /*----------------------------------------------------------------*/ } };

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //注册广播接收 lbm = LocalBroadcastManager.getInstance(getActivity()); lbm.registerReceiver(selectChangedReceiver,new IntentFilter(BroadcastVar.SELECT_CHANGED)); } @Override public void onDestroy() { super.onDestroy(); if(lbm!=null){ lbm.unregisterReceiver(selectChangedReceiver); selectChangedReceiver=null; } }

 【2】取消ViewPager预加载

主要修改BaseFragment

属性添加2个private int isLoad=0;//是否已经加载过private boolean isVisable;//是否可见

修改updateLoadingPage的getMyUrl()方法public String getMyUrl() { if(TextUtils.isEmpty(getUrl())){//本身就不需要联网 isLoad=1; return getUrl(); }else{ if(isVisable){//需要联网,可见 isLoad=2; return getUrl(); }else{//需要联网,不可见 isLoad=3; return null; } }}

重写setUserVisibleHint方法 public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); isVisable=isVisibleToUser;

if(isVisibleToUser && isLoad==3){ //这时候loadingPage一定不为null,因为isLoad==3表示已经初始化过 loadingPage.show(); } }

注意initData解析json的处理JSONObject jsonObject = JSON.parseObject(content);if(jsonObject==null){//因为为了防止ViewPager预加载的时候手动将url置为了空,需要校验一下return;}

 【3】缓存所有Fragment

vpSearch.setOffscreenPageLimit(fenleiFragments.size()-1);

另外FragmentPagerAdapter一定要按照上面的写法。

相关推荐