Dynamically skip pages in ViewPager

≡放荡痞女 提交于 2019-12-05 16:04:07

问题


I got a ViewPager which holds Fragments via FragmentStatePagerAdapter. Let's say the pager initially holds the following pages (Fragments):

A - B - C - D

When the user swipes, he can move from A to B, B to C etc. But there are cases when the user changes some options on the A page, he can move not to B, but C:

A - C - D

Then the user goes back to A, modifies something and that re-enables B:

A - B - C - D

How can i achieve this very dynamic behavior? I cannot add Fragments any time when the user changes something and then re-populate the ViewPager, because it's slow and breaks the flow.


回答1:


We too had the same situation, and we solved this issue by maintaining a boolean ArrayList.The pages here are fragments. On the Fragment, we had written an interface to update the ArrayList.This interface is implemented on the parent activity.On every swipe of the ViewPager we are retrieving the boolean ArrayList. On ViewPager, in setOnPageChangeListener we overrided the onPageSelected.

And here is the piece of code:

pageIndicator.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub

        pagesPageBreakInfoList = activityContext.getBooleanList();
        currentPageNumber = position;

        if (!pagesPageBreakInfoList.get(position)) {
            if (currentPageNumber > previouspagenumber) {
                previouspagenumber = currentPageNumber;
                if (numberofSubmodule == (position + 1)) {
                    viewPager.setCurrentItem(position - 1);
                } else {
                    viewPager.setCurrentItem(position + 1);
                }
            } else {
                previouspagenumber = currentPageNumber;
                viewPager.setCurrentItem(position - 1);
            }
        } else {
            previouspagenumber = currentPageNumber;
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
    }
});


来源:https://stackoverflow.com/questions/19861445/dynamically-skip-pages-in-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!