如何刷新ViewPager

时间:2015-06-17 02:56:58

标签: android android-viewpager

我尝试使用ViewPager Jorte制作日历应用。

当我初始化应用时,我向ViewPager添加了5个页面(每个页面是一个月的日历)。刷完页面后页面到达边缘的左/右侧,然后我重置适配器数据并调用notifyDataSetChanged().

它可以工作,但速度很慢。因为ViewPager会重建这些网页的Fragment

我想要做的是当页面到达左/右边缘时,删除边缘另一边的页面,在边缘旁边添加新页面,然后重复使用其他页面(刷新位置)。 / p>

如何将位置重新分配给已显示的Fragment

MainActivity源代码:

private static final int MAX_PAGE = 5;
private CalendarPagerAdapter mAdapter;
private ViewPager mPager;
private ArrayList<Integer> mItems = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    for (int i=0;i<MAX_PAGE;i++) {
        mItems.add(i-2);
    }

    // Initialize ViewPager
    FragmentManager fm = getSupportFragmentManager();
    mPager = (ViewPager)findViewById(R.id.viewpager);
    mAdapter = new CalendarPagerAdapter(fm, mItems);        
    mPager.setAdapter(mAdapter);
    mPager.setCurrentItem(MAX_PAGE/2, false);
    mPager.setOnPageChangeListener(this);
}

@Override
public void onPageScrollStateChanged(int state) {
    if (state == ViewPager.SCROLL_STATE_IDLE) {
        int position = mPager.getCurrentItem();
        if (position == MAX_PAGE - 1) {
            updateListItemsNext();
            return;
        }
        if (position == 0) {
            updateListItemsPrev();
            return;
        }
    }
}

private void updateListItemsNext() {
    int newData = mItems.get(MAX_PAGE-1) + 1;
    mItems.remove(0);
    mItems.add(newData);        
    mAdapter.destroyAllItem(mPager);        
    mAdapter.notifyDataSetChanged();
    mPager.setCurrentItem(MAX_PAGE-2, false);
}

private void updateListItemsPrev() {
    int newData = mItems.get(0) - 1;
    mItems.remove(MAX_PAGE-1);
    mItems.add(0, newData);
    mAdapter.destroyAllItem(mPager);
    mAdapter.notifyDataSetChanged();
    mPager.setCurrentItem(1, false);        
}

FragmentPagerAdapter源代码:

private List<Integer> mItems = new ArrayList<Integer>();

public CalendarPagerAdapter(FragmentManager fm, ArrayList<Integer> items) {
    super(fm);
    mItems = items;
}

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

@Override
public int getCount() {
    return mItems.size();
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

public void destroyAllItem(ViewPager pager) {
    for (int i = 0; i < getCount() - 1; i++) {
        try {
            Object obj = this.instantiateItem(pager, i);
            if (obj != null)
                destroyItem(pager, i, obj);
        } catch (Exception e) {
        }
    }
}

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

    if (position <= getCount()) {
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.remove((Fragment) object);
        trans.commit();
    }
}

0 个答案:

没有答案