public class OffFilesViewPagerAdapter extends FragmentPagerAdapter { private FragmentManager fm; private List<OffFileBean> mData; int position; List<OffFileBean> outFileBeans = new ArrayList<>(); List<OffFileBean> inFileBeans = new ArrayList<>(); public OffFilesViewPagerAdapter(FragmentManager fm, List<OffFileBean> list) { super(fm); this.fm = fm; this.mData = list; } @Override public Fragment getItem(int position) { this.position = position; for (OffFileBean bean : mData) { if ("outlibrary".equals(bean.getStatus())) { outFileBeans.add(bean); } else if ("inlibrary".equals(bean.getStatus())) { inFileBeans.add(bean); } } if (position == 0) { return OffFileFragment.newInstance(inFileBeans); } else if (position == 1) { return OffFileFragment.newInstance(outFileBeans); } else if (position == 2) { return OffFileFragment.newInstance(mData); } return null; } //注意重载方法 要选择有ViewGroup 的 @Override public Object instantiateItem(ViewGroup container, int position) { if (position == 0)//去掉第一个的缓存 removeFragment(container, position); OffFileFragment itemFragment = (OffFileFragment) super.instantiateItem(container, position); LogUtils.error("instantiateItem position--------------" + position); return itemFragment; } private void removeFragment(ViewGroup container, int index) { String tag = getFragmentTag(container.getId(), index); Fragment fragment = fm.findFragmentByTag(tag); if (fragment == null) return; FragmentTransaction ft = fm.beginTransaction(); ft.remove(fragment); ft.commit(); ft = null; fm.executePendingTransactions(); } private String getFragmentTag(int viewId, int index) { try { Class<FragmentPagerAdapter> cls = FragmentPagerAdapter.class; Class<?>[] parameterTypes = {int.class, long.class}; Method method = cls.getDeclaredMethod("makeFragmentName", parameterTypes); method.setAccessible(true); String tag = (String) method.invoke(this, viewId, index); return tag; } catch (Exception e) { e.printStackTrace(); return ""; } } @Override public int getItemPosition(Object object) { return POSITION_NONE; } @Override public int getCount() { return 3;//有几个页面 } public void setList(List<OffFileBean> list) { mData = list; notifyDataSetChanged(); } }
ViewPageAdapter 可以刷新配合fragment
最新推荐文章于 2023-05-12 11:13:43 发布