I recently made an app using SlidingTabLayout
with two tabs. I referred this link
However I had to modify it slightly. I had to add a button which locks the sliding of the tabs. And unlock it when it is clicked again. So I just can't get the tabs to not slide.
I checked out this question Disable swiping between tabs. But he is using some other library to do it and it's no longer supported. I'm using the default one. And in that question the CustomViewPager extends android.support.v4.view.ViewPager. In my project ViewPagerAdapter extends FragmentStatePagerAdapter.
Any help would be very useful. Thank you.
the method has slightly different on the time of its invocation, you just have to setup your tabitem to disable its click after all viewpager fragment already added.
Android ViewPager ViewPager with TabLayout A TabLayout can be used for easier navigation. You can set the tabs for each fragment in your adapter by using TabLayout. newTab() method but there is another more convenient and easier method for this task which is TabLayout. setupWithViewPager() .
It is possible to use a TabLayout without a ViewPager by using a TabLayout. OnTabSelectedListener . For navigation within an Activity , manually populate the UI based on the tab selected.
You can just make a custom ViewPager which extends the ViewPager and set a method that disables and enables the swiping.
You can do that by adding a class like the one below to your code. Then instead of using ViewPager just use CustomViewPager in your code:
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;
}
}
You can disable/enable swiping by calling: setPagingEnabled(boolean enabled).
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