Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit ViewPager page changes to one per gesture

When you use setPageMargin to set a negative margin to your ViewPager the pages will become smaller than the full width of the ViewPager. Side effect of this is that user can then use one long gesture to change the active page more than once.

Is there a way to prevent a single gesture changing the page more than once?

I have tried to stop the motion events from reaching the the pager but you will have to let the MotionEvent.ACTION_UP to go through or the pager doesn't work correctly. The up also triggers velocity based calculations in the ViewPager and causes an extra page change when it is triggered.

Either I need to reset the velocity of the drag or I need somehow prevent additional page changes.

I have used the method described in this question's answer to stop the scrolling but the described ACTION_UP problem prevents me from completing it. Is it possible to disable scrolling on a ViewPager

Any ideas?

like image 753
Juhani Avatar asked Dec 06 '13 13:12

Juhani


1 Answers

I don't know exactly what are you trying to achieve here, but instead of using a negative margin wouldn't be better to user getPageWidth() on the PagerAdapter to make the pages width smaller than the ViewPager width?

Edit

Ok, I came up with a 'hacky' solution, consisting in copying the source code from ViewPager into your project and modifying a little bit performDrag(), so it will not drag more than one page.

This Hack works with some assumptions:

  1. It uses getPageWidth() to determine the relative size of the pages
  2. All pages must return the same getPageWidth() value

You can check the sample project, and the ViewPager modifications diff

It not a beautiful or elegant solution, but it works, and I'll finally close a 3 months old ticket with this same issue, so manager happy :)

like image 186
luciofm Avatar answered Oct 23 '22 23:10

luciofm