Make tabs in SlidingTabLayout not slides

I recently made an application using SlidingTabLayout with two tabs. I referred to this link

Sliding Tabs

However, I had to change it a bit. I had to add a button that locks the sliding tabs . And unlock it when it is clicked again. So I just can't get the tabs to not slide.

I checked this question Disable scrolling between tabs . But he uses some other library for this, and it is no longer supported. I use by default. And in this question, CustomViewPager extends android.support.v4.view.ViewPager. In my project, the ViewPagerAdapter extends the FragmentStatePagerAdapter.

Any help would be very helpful. Thanks.

+5
source share
1 answer

You can simply create your own ViewPager, which extends the ViewPager and sets up a method that disables and enables scrolling.

You can do this by adding a class like the one below to your class. 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 scrolling by calling: setPagingEnabled(boolean enabled).

+1
source

Source: https://habr.com/ru/post/1238934/


All Articles