With ViewPager and PagerAdapter, how do I remove the item at position "0" with the current item "0"?

I am using ViewPager with PagerAdapter from android.support.v4.view.ViewPager library, I want to remove null Item number if List when my current Item is also null. I can uninstall it without any error, but the screen doesn't refresh instantly. If I go to point 2 and return, then the null element is updated. Can anyone 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 think the problem must happen in the function call

setCurrentItemInternal(newCurrItem, false, true);

      

with parameter newCurrItem '0' and mCurItem '0' in ViewPager.java. Also, the problem is that it must be either completeScroll (); or scrollTo (destX, 0); at the end setCurrentItemInternal ();

+3


source to share


1 answer


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

public void setOffscreenPageLimit (int limit)

Specify the number of pages to be kept on either side of the current page in the pending view hierarchy. Pages outside this limit will be recreated from the adapter if necessary.

This is suggested as an optimization. If you know ahead of time the page numbers that you will need to maintain or have lazy-loading mechanisms into place on your pages, tweaking this setting can have a perceived smoothness in paging and interaction animations. If you have a small number of pages (3-4) that you can save at the same time, less time will be spent on layout for newly created view subtypes as the user's pages are back and forth.

You should keep this limit low, especially if your pages have complex layouts. By default, this parameter is 1.

Parameter limitation. How many pages will be kept on the screen in standby state.



Set the OffScreenPageLimit value to zero and then remove the item. You can set OffScreenPageLimit back to 1 (default).

0


source







All Articles