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
To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
Find the view pager that will allow the user to swipe between fragments. Create an adapter that knows which fragment should be shown on each page. Set the adapter onto the view pager. Give the TabLayout the ViewPager.
ViewPager - How It Works The idea is that ViewPager works with a PageAdapter to supply the Views that represent each page. The basic idea is that ViewPager keeps track of what page should be visible on the screen and then asks the PagerAdapter to get the View hierarchy that needs to be displayed.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With