gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:10:49 32 4
gpt4 key购买 nike

我想根据条件禁用 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com