Unlimited/Dynamic ViewPager in both directions

前端 未结 2 1622
死守一世寂寞
死守一世寂寞 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:02

    Will this help,

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }
    
        @Override
        public Fragment getItem(int position) {
            return getFragmentBasedOnPosition(position);
        }
    
        private Fragment getFragmentBasedOnPosition(int position) {
            int fragmentPos = position % 3; // Assuming you have 3 fragments
            switch(fragmentPos) {
                case 1:
                return Fragment1.newInstance();
                case 2:
                return Fragment2.newInstance();
                case 3:
                return Fragment3.newInstance();
            }
        }
    }
    

    and then,

    mPager.setCurrentItem((int)(Integer.MAX_VALUE/2)); // assuming mPager is your ViewPager

提交回复
热议问题