android - 自定义 PagerTabStrip 如何通过单击禁用切换选项卡 - 不起作用

标签 android android-layout android-activity android-5.0-lollipop pagertabstrip

我想根据条件禁用 PagerTabStrip 的“通过单击选项卡名称切换选项卡”。

我认为以下代码是正确的,但我仍然可以通过单击选项卡名称来切换选项卡。我能够通过自定义 ViewPager 滑动来禁用选项卡切换,但我也想通过单击选项卡名称来禁用。

我的自定义 PagerTabStrip

package com.blah;

import android.content.Context;
import android.support.v4.view.PagerTabStrip;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomPagerTabStrip extends PagerTabStrip {

    private boolean isTabSwitchEnabled;

    public CustomPagerTabStrip(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.isTabSwitchEnabled = true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        System.out.println("ENable?? "+isTabSwitchEnabled); // it prints out false or true based on what I have set
        if (this.isTabSwitchEnabled) {
            return super.onInterceptTouchEvent(event);
        } else {
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.isTabSwitchEnabled) {
            return super.onTouchEvent(event);
        }
        return false;
    }

    public void setTabSwitchEnabled(boolean isSwipeEnabled) {
        this.isTabSwitchEnabled = isSwipeEnabled;
    }
}

我通过使用禁用它:

CustomPagerTabStrip pagerTabStrip = (CustomPagerTabStrip) findViewById(R.id.tabtitle);
pagerTabStrip.setTabSwitchEnabled(false);

我的布局:

<com.blah.CustomViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.blah.CustomPagerTabStrip
            android:id="@+id/tabtitle"
            style="@style/viepagertitlestrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="@color/primary"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            >

        </com.blah.CustomPagerTabStrip>
    </com.blah.CustomViewPager>

最佳答案

通过返回true来使用onInterceptTouchEvent()中的触摸事件。我认为它可以完成这项工作

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    System.out.println("ENable?? "+isTabSwitchEnabled); // it prints out false or true based on what I have set
    if (this.isTabSwitchEnabled) {
        return super.onInterceptTouchEvent(event);
    } else {
        return true;
    }
}

关于android - 自定义 PagerTabStrip 如何通过单击禁用切换选项卡 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30282590/

相关文章:

java - gradle 2.2.2,android studio,没有方法签名:java.lang.Boolean.call()适用于参数类型:

android - 固定 TextView 的宽度

android - 如何在底部导航 View 中添加分隔线

java - 如何在所有包含的线程完成之前阻止子例程结束?

OnClickListener.onClick中出现NullPointerException时Android卡死(其他任务无法启动)

Android launchMode ="singleTask"和 Intent-filters

android - 现在必须显式声明注释处理器。 - compiler-1.1.1.jar (android.arch.lifecycle :compiler:1. 1.1)

javascript - 电话间隙 : is it possible to detect swipeLeft and SwipeRight touch from Javascript?

android - 使用 fragment 的动态方式

android - TextInputLayout float 标签自定义字体和edittext自定义字体