码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager与Fragment刷新数据

时间:2015-07-28 20:23:19      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

唉,每次都忘记怎么写,还是老老实实记一下吧!

public class MDTabViewPagerAdapter extends FragmentStatePagerAdapter {

    private static final String TAG = MDTabViewPagerAdapter.class.getSimpleName();
    private List list;
    private FragmentManager fm;

    public MDTabViewPagerAdapter(FragmentManager fm, List list) {
        super(fm);
        this.fm = fm;
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment =  (Fragment)list.get(position);
        return fragment;
    }

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

    public void setFragments(ArrayList<Fragment> fragments) {
        if ((list == null) && (fm == null) || (fragments == null)) {
            LogUtil.w(TAG, "setFragments is fail. params is null");
            return;
        }
        FragmentTransaction ft = fm.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            Fragment fragment = (Fragment) list.get(i);
            ft.remove(fragment);
        }
        ft.commit();
        ft = null;
        fm.executePendingTransactions();

        this.list.addAll(fragments);
        notifyDataSetChanged();
    }
    /*
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            return fragment;
        }
    
        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    */

}

 

ViewPager与Fragment刷新数据

标签:

原文地址:http://www.cnblogs.com/Mr-Mo/p/4684029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!