ViewPageAdapter 可以刷新配合fragment

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值