Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何禁用PagerTabStrip的Tab click事件_Android_Android Viewpager - Fatal编程技术网

Android 如何禁用PagerTabStrip的Tab click事件

Android 如何禁用PagerTabStrip的Tab click事件,android,android-viewpager,Android,Android Viewpager,我想在我的页面上禁用tab click/touch事件tab Strip。基本上,我只希望用户能够刷卡。我想要选项卡的外观,但不要触摸/单击事件。我不确定下面的xml中是否有可配置的内容。或者如果代码中有我可以做的事情。我尝试过覆盖这部连续剧的ontouch和onclick监听器。但是没有运气 <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" x

我想在我的页面上禁用tab click/touch事件tab Strip。基本上,我只希望用户能够刷卡。我想要选项卡的外观,但不要触摸/单击事件。我不确定下面的xml中是否有可配置的内容。或者如果代码中有我可以做的事情。我尝试过覆盖这部连续剧的ontouch和onclick监听器。但是没有运气

<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类中包含的两个文本视图(上一个选项卡和下一个选项卡的文本)上,并且PagerTabStrip上目前没有API可直接访问/重写这些侦听器。以下是解决此问题的解决方案(也不会陷入内部PagerTabStrip实现的杂草中)

我验证了以下工作:

创建自己的PagerTabStrip,并通过返回true使用onInterceptTouchEvent()中的触摸事件。这将阻止PagerTabStrip的内部onClick侦听器接收触摸事件并进行tab切换

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,以允许正常触摸事件到达当前选项卡片段):

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

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

Steve B的答案几乎可以使用了,但TabStrip中只缺少onTouchEvent()覆盖(与viewpager相同)。如果没有它,它仍然会收到点击和更改页面(至少在Android L上是这样)。

我也遇到了同样的问题,你知道吗??下面的解决方案对我不起作用。请尝试这样重写此方法:public MyPagerTabStrip(上下文上下文,属性集attrs){super(上下文,attrs);this.mPrevText.setOnClickListener(null);this.mNextText.setOnClickListener(null);}如何使用android.support.design.widget.TabLayout实现同样的功能?谢谢
<com.mypackage.MyViewPager
    ...
    <com.mypackage.MyPagerTabStrip
     ...