How Do I Disable The Tab Click Event For A Pagertabstrip
Solution 1:
Overriding the onClick listener on PagerTabStrip does nothing because the onClick listeners are actually on two TextViews (the text for the previous and next tabs) contained within the PagerTabStrip class, and there is currently no API on PagerTabStrip to directly access/override those listeners. The following is solution that gets around this problem (and also doesn't get into the weeds of the internal PagerTabStrip implementation).
I verified that the following works:
Create your own PagerTabStrip and consume the touch event in onInterceptTouchEvent() by returning true. This will prevent either of the PagerTabStrip's internal onClick listeners from receiving touch event and doing the tab switch.
publicclassMyPagerTabStripextendsPagerTabStrip {
privateboolean isTabSwitchEnabled;
publicMyPagerTabStrip(Context context, AttributeSet attrs) {
super(context, attrs);
this.isTabSwitchEnabled = true;
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent event) {
if (this.isTabSwitchEnabled) {
returnsuper.onInterceptTouchEvent(event);
} else {
returntrue;
}
}
publicvoidsetTabSwitchEnabled(boolean isSwipeEnabled) {
this.isTabSwitchEnabled = isSwipeEnabled;
}
}
I assume that you'll also want to disable the ViewPager swiping that would also result in a tab switch. The following code does that (here, you have to return false in onTouch() and onInterceptTouch() instead of true to allow normal touch events to reach your current tab fragment):
publicclassMyViewPagerextendsViewPager {
privateboolean isSwipeEnabled;
publicMyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.isSwipeEnabled = true;
}
@OverridepublicbooleanonTouchEvent(MotionEvent event) {
if (this.isSwipeEnabled) {
returnsuper.onTouchEvent(event);
}
returnfalse;
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent event) {
if (this.isSwipeEnabled) {
returnsuper.onInterceptTouchEvent(event);
}
returnfalse;
}
publicvoidsetPagingEnabled(boolean isSwipeEnabled) {
this.isSwipeEnabled = isSwipeEnabled;
}
}
Remember to change the XML file to reference these new classes:
<com.mypackage.MyViewPager
...
<com.mypackage.MyPagerTabStrip
...
Solution 2:
Answer of Steve B is almost working, only the onTouchEvent() override is missing from the TabStrip (the same as for the viewpager). Without it it still receives the clicks and change page (at least on Android L).
Post a Comment for "How Do I Disable The Tab Click Event For A Pagertabstrip"