Redefining the onClick listener on PagerTabStrip does nothing, because onClick listeners are actually on the two TextViews (text for the previous and next tab) contained in the PagerTabStrip class, and there is currently no API on the PagerTabStrip for directly accessing / redefining these listeners. Below is a solution that covers this problem (and also does not fall into the weeds of the internal implementation of PagerTabStrip).
I checked that the following works:
Create your own PagerTabStrip and use the touch event in onInterceptTouchEvent (), returning true. This will prevent one of the PenderTabStrip from getting onClick embedded listeners and clicking the tab switch.
public class MyPagerTabStrip extends PagerTabStrip { private boolean isTabSwitchEnabled; public MyPagerTabStrip(Context context, AttributeSet attrs) { super(context, attrs); this.isTabSwitchEnabled = true; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.isTabSwitchEnabled) { return super.onInterceptTouchEvent(event); } else { return true; } } public void setTabSwitchEnabled(boolean isSwipeEnabled) { this.isTabSwitchEnabled = isSwipeEnabled; } }
I assume that you also want to disable ViewPager scrolling, which will also cause tabs to switch. The following code does this (here you should return false in onTouch () and onInterceptTouch () instead of true so that ordinary touch events can reach your current tab fragment):
public class MyViewPager extends ViewPager { private boolean isSwipeEnabled; public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); this.isSwipeEnabled = true; } @Override public boolean onTouchEvent(MotionEvent event) { if (this.isSwipeEnabled) { return super.onTouchEvent(event); } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.isSwipeEnabled) { return super.onInterceptTouchEvent(event); } return false; } public void setPagingEnabled(boolean isSwipeEnabled) { this.isSwipeEnabled = isSwipeEnabled; } }
Remember to modify the XML file to reference these new classes:
<com.mypackage.MyViewPager ... <com.mypackage.MyPagerTabStrip ...
source share