How to disable the Click click event for PagerTabStrip

I would like to disable the click / touch tab on my PagerTabStrip. Mostly I want users to be able to scroll. I want tabs to appear, but not touching / clicking events. I am not sure if there is anything configurable in xml below. Or if there is something in the code that I can do. I tried overriding ontouch and onclick tape listening. but no luck.

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myId"> <android.support.v4.view.PagerTabStrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:id="@+id/tabStripID" android:background="@color/dark_blue" android:textColor="@color/text"/> </android.support.v4.view.ViewPager> 
+3
source share
2 answers

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 ... 
+5
source

Answer of Steve B almost works, only the onTouchEvent () override is missing in the TabStrip (same as for the viewpager). Without it, it still gets clicks and changes the page (at least on Android L).

+1
source

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


All Articles