4

Solution-1: By specifying a very large number as the actual count, but maps them to the actual range of the dataset/pageset

ViewPager as a circular queue / wrapping

Problem: It doesn't work if there are less than 4 items.


Solution-2 Adding 2 extra items i.e. last item in the first and first item in the last and onPageSelected, depending on the position values, set the current item

Implementing Circular Scrolling In PagerAdapter

Problem: Extra pager indicators and extra fragment instances.


Solution-3: Using the onTouchListener on ViewPager

Implementing Circular Scrolling In PagerAdapter

Problem: The first MotionEvent is always null. Is it because of the ViewPager's own onTouchListener implementation?

Is there a proper solution for this?

Or even if the 3rd solution works fine, it would be great.

Community
  • 1
  • 1
Archie.bpgc
  • 22,633
  • 38
  • 142
  • 219
  • I am trying to make circular viewpager too. I have implemented it. using 2 solution. Now the problem is that fragments doesn't work after first round of scroll. they work fine in first round. but after first round they don't work. Buttons aren't working. can you please look at my question http://stackoverflow.com/questions/34481219/circular-viewpager-fragments-dont-work-as-they-supposed-to-after-first-round – Zeeshan Shabbir Dec 28 '15 at 05:19

1 Answers1

0

I use a combination of Solution 2 and Solution 3, I use the on touch listener of the pager to watch for swipes and see if I need to show the copy of the first or last page based on the current page

example:

mPager.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    dxInitial=event.getX();
//                  
                }else if(event.getAction() == MotionEvent.ACTION_MOVE){
//                  
                    if(event.getX() - dxInitial > 50 && currentPage==1){

                    }else if(dxInitial - event.getX() > 50 && currentPage==5){
                        mPager.setCurrentItem(0,false);
                    }else if(dxInitial - event.getX() > 50 && currentPage==6){
                        mPager.setCurrentItem(1,false);
                    }else if(event.getX() - dxInitial > 50 && currentPage==0){
                        mPager.setCurrentItem(5,false);
                    }else if(event.getX() - dxInitial > 100 && currentPage==6){
//                      
                        mPager.setCurrentItem(1,false);
                    }
                }

                return false;
            }
        }); 

you have extra pager indicators but the user should never see or notice them, just set smoothScroll to false when you change the page

tyczj
  • 66,691
  • 50
  • 172
  • 271
  • @tyczi If you are using a `touchListener` why do you need 2 extra items again? Can you please check my [solution](http://stackoverflow.com/questions/22939249/circular-viewpager-implementation/22953785#22953785) and tell if there are any flaws. Thank you – Archie.bpgc Apr 09 '14 at 05:48
  • because you can smooth scroll to the last page and then on the last page is when you switch to the copy during the swipe. This way makes it seem more like a natural viewpager. If you just change page on the last index to the first index it is not as smooth looking – tyczj Apr 09 '14 at 13:22
  • How would you make this work with a dynamic size ViewPager? – Peter Chappy Oct 29 '15 at 19:11