Unlimited/Dynamic ViewPager in both directions

前端 未结 2 1624
死守一世寂寞
死守一世寂寞 2021-02-02 13:59

There is one use-case of ViewPager I\'ve never seen pretty implemented.

ViewPager is more or less static structure. It\'s not so hard to add Pa

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 15:00

    Using negative position values stands very far from natural order of ViewPager usage and is not the Adapter issue at all. Please have a look into the source code of ViewPager in the support package source in Android SDK. For example, here is a setCurrentItemInternal private implementation:

    void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
        if (mAdapter == null || mAdapter.getCount() <= 0) {
            setScrollingCacheEnabled(false);
            return;
        }
        if (!always && mCurItem == item && mItems.size() != 0) {
            setScrollingCacheEnabled(false);
            return;
        }
    
        if (item < 0) {
            item = 0;
        } else if (item >= mAdapter.getCount()) {
            item = mAdapter.getCount() - 1;
        }
        final int pageLimit = mOffscreenPageLimit;
        if (item > (mCurItem + pageLimit) || item < (mCurItem - pageLimit)) {
            // We are doing a jump by more than one page.  To avoid
            // glitches, we want to keep all current pages in the view
            // until the scroll ends.
            for (int i=0; i

    As you can see, ViewPager explicitly assumes no negative position values.

提交回复
热议问题