I am creating an application contains of view pager with swipe-able tabs i want to do some operations inside view pager and at the time of operations i want to disable the swipe view after operations done enable the swipe view can any one tell me how to do this and my activity was extending to the fragment activity thanks in advance
Asked
Active
Viewed 2.0k times
15
-
You've asked the same question just 2 hours ago! – Hans1984 Jun 23 '15 at 10:45
-
Yes dude here i am using fragment activity that's my main problem @Hans1984 – lakshman sundeep Jun 23 '15 at 10:47
-
So why do you ask it again??? This is not the purpose of this site. – Hans1984 Jun 23 '15 at 10:48
-
It's still very similar. Well, I'll let the mods take care of. – Hans1984 Jun 23 '15 at 10:50
-
I have found a great solution that worked for me http://stackoverflow.com/a/42687397/4559365 – Pradeep Kumar Kushwaha Mar 09 '17 at 05:28
2 Answers
41
Create a new class which named CustomViewPager
. The class inherits from ViewPager
and includes a new method called setPagingEnabled
. To enable / disable the swiping, just overwrite two methods: onTouchEvent
and onInterceptTouchEvent
. Both return false if the paging is disabled.
Here is the full code:
package com.yourpackage;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Then in your layout.xml file replace any <com.android.support.V4.ViewPager>
tags with <com.yourpackage.CustomViewPager>
tags like below.
<com.yourpackage.CustomViewPager
android:id="@+id/photosViewPager"
android:layout_height="match_parent"
android:layout_width="match_parent" />
Now use CustomViewPager
instead of ViewPager
in your activity.
And that's it. Now, anytime to disable the swiping, just need to call the setPagingEnabled
method with false.
CustomViewPager mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setPagingEnabled(false);

Priyank Patel
- 12,244
- 8
- 65
- 85
-
Here i am using the activity extends Fragment activity but here t was using the view pager.Can i do this in Fragment activity @Priyank – lakshman sundeep Jun 23 '15 at 10:32
-
-
I didn't get any overridden methods for fragment activity can u help me please @Priyank – lakshman sundeep Jun 23 '15 at 10:37
-
-
You need to create new class called `CustomViewPager` & use that `CustomViewPager` in your fragment activity & xml file instead of `ViewPager`. – Priyank Patel Jun 23 '15 at 10:55
-
Worked like a charm! Basically its better to extend and then manage the code in a separate class then to do the dirty work in the Activity. Good Thanks! – sud007 Jun 02 '17 at 09:16
-
one more function need to override to completely disable swiping onInterceptTouchEvent – kyawagwin Dec 09 '17 at 06:27
-
-
Overriding `onInterceptTouchEvent()` will prevent child views to receive input. In most cases this is __not__ the intended behavior. – Alex Semeniuk Jul 16 '19 at 14:32
-
@PriyankPatel How you achieve it? I have the same problem. I have to disable specific area of my 1st fragment. In my view pager I have total 5 fragment. – Menu Jul 22 '21 at 14:24
-5
Try below methods
To disable swiping call
viewPager.beginFakeDrag();
To enable swiping call
viewPager.endFakeDrag();

Chandrakanth
- 3,711
- 2
- 18
- 31