简体   繁体   中英

With ViewPager and PagerAdapter, how to remove Item at position '0' with Current Item of '0'?

I am using the ViewPager with PagerAdapter from android.support.v4.view.ViewPager Library, I want to remove the number zero Item if List, when my current Item is also number zero. I can remove it without any error, but the screen does not refresh instantly. If I go to Item 2 and come back, then the Item zero is updated. Can anybody solve this problem?

I am using the remove-function below:

public View removePage(int position) {
        if ((position < 0) || (position >= getSize()) || (getSize()<=1)) {
            return null;
        } else {
            if (position == mPager.getCurrentItem()) {
                if(position == (getSize()-1)) {
                    mPager.setCurrentItem(position-1);
                } else if (position == 0){
                    mPager.setCurrentItem(1);
                }
            }
            View tempView = myPagerAdapter.mListViews.remove(position);
            myPagerAdapter.notifyDataSetChanged();
            return tempView;
        }
    }

And I guess the problem should happened in the function call of

setCurrentItemInternal(newCurrItem, false, true);

with the parameter of newCurrItem of '0', and mCurItem of '0' in ViewPager.java. Further more, the trouble should be in either completeScroll(); or scrollTo(destX, 0); at the end of setCurrentItemInternal();

From http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int )

public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

Parameters limit How many pages will be kept offscreen in an idle state.

Set the OffScreenPageLimit to zero and than remove the item. You can set OffScreenPageLimit to 1 (default) again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM