I have ViewPager and below it I have 10 buttons. By clicking on button, for example #4, the pager goes immediately to page #4 by mPager.setCurrentItem(3);
. But, I want to disable the paging by swiping with finger horizontally. Thus, the paging is done ONLY by clicking on the buttons.
So, how I can disable the swiping with finger?
Android – How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically
androidandroid-viewpager
Related Topic
- Android – How to stop EditText from gaining focus at Activity startup in Android
- Android – How to display an alert dialog on Android
- Android – How to prevent a dialog from closing when a button is clicked
- Android – ViewPager PagerAdapter not updating the View
- Android – How to programmatically show next view in ViewPager
- Android – How to determine when Fragment becomes visible in ViewPager
- C# – NHibernate nested class mapping issue
Best Answer
You need to subclass
ViewPager
.onTouchEvent
has a lot of good things in it that you don't want to change like allowing child views to get touches.onInterceptTouchEvent
is what you want to change. If you look at the code forViewPager
, you'll see the comment:Here's a complete solution:
First, add this class to your
src
folder:Next, make sure to use this class instead of the regular
ViewPager
, which you probably specified asandroid.support.v4.view.ViewPager
. In your layout file, you'll want to specify it with something like:This particular example is in a
LinearLayout
and is meant to take up the entire screen, which is whylayout_weight
is 1 andlayout_height
is 0dp.And
setMyScroller();
method is for smooth transition