bmurmistro
bmurmistro

Reputation: 1080

How do I disable the Tab click event for a PagerTabStrip

I would like to disable the tab click / touch event on my PagerTabStrip. Basically I only want users to be able to swipe. I want the appearance of the tabs, but no touching/clicking events. I'm not sure if there is something configurable in the xml below. Or if there is something in code that I can do. I've tried overriding the strip's ontouch and onclick listeners. 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>

Upvotes: 3

Views: 3127

Answers (2)

codwell
codwell

Reputation: 749

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).

Upvotes: 1

Steve B
Steve B

Reputation: 1246

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.

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'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):

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 change the XML file to reference these new classes:

<com.mypackage.MyViewPager
    ...
    <com.mypackage.MyPagerTabStrip
     ...

Upvotes: 5

Related Questions