TabLayout remove unnecessary scrolling
Actually you are dealing with Scrolling issue. Yes. the thing is, TabLayout Extends HorizontalScrollView. try Something like this.
public class CustomTabLayout extends TabLayout {
public CustomTabLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public CustomTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTabLayout(Context context) {
super(context);
init(context);
}
void init(Context ctx){
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Do not allow touch events.
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Do not allow touch events.
return false;
}
}
I think it's worth using TabLayoutMediator
and it will look like this
TabLayoutMediator(tabs, view_pager, true, false) { tab, position ->
tab.text = ""
}.attach()
you can try this code
tabLayout.setupWithViewPager(viewPager);
// little hack to prevent unnecessary tab scrolling
tabLayout.clearOnTabSelectedListeners();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition(), false);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) { }
@Override
public void onTabReselected(TabLayout.Tab tab) { }
});
or you can also do this directly for the last line
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition(), false);
}
});