如何禁用PagerTabStrip的Tab单击事件 [英] How do I disable the Tab click event for a PagerTabStrip

查看:62
本文介绍了如何禁用PagerTabStrip的Tab单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用PagerTabStrip上的选项卡单击/触摸事件.基本上,我只希望用户能够滑动.我想要选项卡的外观,但没有触摸/单击事件.我不确定下面的xml中是否有可配置的内容.或者,如果代码中有什么我可以做的.我尝试覆盖该条的ontouch和onclick侦听器.但没有运气.

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>

推荐答案

在PagerTabStrip上覆盖onClick侦听器没有任何作用,因为onClick侦听器实际上位于PagerTabStrip类中包含的两个TextView(上一个和下一个选项卡的文本)上,目前,PagerTabStrip上没有API可以直接访问/覆盖这些监听器.以下是解决此问题的解决方案(并且也没有涉及内部PagerTabStrip实现的杂草).

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:

创建自己的PagerTabStrip,并通过返回true在onInterceptTouchEvent()中使用touch事件.这样可以防止PagerTabStrip的内部onClick侦听器中的任何一个接收触摸事件并进行选项卡切换.

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;
    }
}

我假设您还将要禁用ViewPager滑动,这也会导致标签页切换.下面的代码可以做到这一点(在这里,您必须在onTouch()和onInterceptTouch()中返回false而不是true才能使正常的触摸事件到达您当前的标签片段)

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;
    }
}

请记住更改XML文件以引用以下新类:

Remember to change the XML file to reference these new classes:

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

这篇关于如何禁用PagerTabStrip的Tab单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆