如何在Android中禁用TabLayout上的点击

38
我想在我的应用程序中使用TabLayout来使用fragmentViewPager,但是我想禁用TabLayout上的点击以切换fragment,只需滑动即可在fragmenst之间切换。 我编写了下面的代码,但对我不起作用,当单击TabLayout项目时,会转到该fragmentMainActivity XML :
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="@string/app_namefull"
                    android:textSize="23sp" />

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="72dp"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/white"
                app:tabMode="fixed" />
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.CoordinatorLayout>

主活动java:

public class Main_Page extends AppCompatActivity {

    private CollapsingToolbarLayout mCollapsingToolbarLayout;
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main__page);

        mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
        //mCollapsingToolbarLayout.setTitle(getResources().getString(R.string.app_name));

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
        for(int i = 0; i < tabStrip.getChildCount(); i++) {
            tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
        }
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    /**
     * Adding custom view to tab
     */
    private void setupTabIcons() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText(R.string.free_fragment_title);
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_download_image, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText(R.string.paid_fragment_title);
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_paid_download_image, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText(R.string.pdf_fragment_title);
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_pdf_icon, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
    }

    /**
     * Adding fragments to ViewPager
     * @param viewPager
     */
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new free_fragment(), "رایگان ها");
        adapter.addFrag(new paid_fragment(), "پرداختی ها");
        adapter.addFrag(new pdf_fragment(), "مقالات");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

为了禁用TabLayout上的点击功能,我使用了这段代码:

LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}

我该如何解决这个问题?谢谢大家<3


设置 android:clickable="false" - Tim
2
@TimCastelijns,对我不起作用。 - Mohammad Nouri
可能是 Disable TabLayout 的重复问题。 - C.Schone
7个回答

55

在调用setupWithViewPager之前,你正在访问选项卡,这就是为什么你的代码无法工作的原因。所以先设置选项卡,然后再设置触摸监听器的代码。

尝试这样做:

tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
    for(int i = 0; i < tabStrip.getChildCount(); i++) {
        tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

41

对于 ViewPager2(最低版本为v1.1.0),您可以在TabLayoutMediator中完成此操作。

new TabLayoutMediator(tabLayout, pager,
        (tab, position) -> {
            tab.view.setClickable(false);
        }
).attach();

build.gradle 文件中:

com.google.android.material:material:1.1.0-rc02

只是想补充一下,我正在使用数据绑定,在这之后我尝试重新启用它们,但是binding.tablayout.touchables.forEach { it.isClickable = true }没有起作用。解决方案是对每个选项卡使用binding.tablayout.getTabAt(0)?.view?.isClickable = true - The Mark

24

可触摸数组列表将解决这个问题。

声明该活动。

var touchableList: ArrayList<View>? = ArrayList()

禁用所有选项卡

touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
使选项卡可用。
touchableList?.get(0)?.isEnabled = true

17

不必使用线性布局,您只需将此添加到活动中即可:

private void setUntouchableTab () {
    tablayout.setupWithViewPager(viewpager, true);
    tablayout.clearOnTabSelectedListeners();
    for (View v : tablayout.getTouchables()) {
        v.setEnabled(false);
    }
}

1
这应该是被接受的答案 :) 我在这里找到了更好、更快的解决方案!谢谢 - Romain Barbier
4
这个很好用,但我无法让 setEnabled(true) 重新启用点击。 - Luke Galea
首先,将tablayout.getTouchables()保存到私有字段中,以存储哪些元素是可触摸的。因此,当您使用true标志调用它时,您将遍历已定义的视图。 - undefined

8
tabLayout.clearOnTabSelectedListeners();

5
你好,欢迎来到StackOverflow。请为你的回答添加一些解释,使其对其他用户更有价值。请参阅http://stackoverflow.com/help/how-to-answer。 - wmk
如果你使用的是 Android Material Components 中的 TabLayout,那么它应该可以在没有 API 24 限制的情况下工作。 - Vitor Hugo Schwaab
2
这不会禁用选项卡的点击。被点击的选项卡将被选中,但ViewPager不会收到此点击的通知。 - Ahmed Mostafa

4

这是对我有效的做法:

class LockableTabLayout : TabLayout {

    var swipeable: Boolean = true

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean = !swipeable
}

1

这里的答案,当您填写完选项卡项目或已在XML中定义选项卡项目时,禁用选项卡项目的单击操作。

e.g:

        <android.support.design.widget.TabLayout
            android:id="@+id/tabla_quiz_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:text="1"
                android:layout_height="wrap_content"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:text="2"
                android:layout_height="wrap_content"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:text="3"
                android:layout_height="wrap_content"/>

        </android.support.design.widget.TabLayout>

但是,如果您使用Observable数据或动态选项卡时有不同的情况,例如:
        // QuizFragment.kt
        quizListLive.observe(this@QuizFragment, Observer { quizList ->

            quizList?.takeIf { list -> list.isNotEmpty() }?.let {

                // this is where new fragment added
                it.forEachIndexed { j, quiz ->
                    mViewPagerAdapter?.addFragment(
                            fragment = QuizFragmentSub.newInstance(quiz = quiz),
                            title = (j + 1).toString()
                    )
                }

                // notify the viewpager, I'm using kotlin extension to call widget id
                vp_quiz_content.adapter?.notifyDataSetChanged()

                // then, childCount will have size > 0
                // disable tablayout click
                val tabStrip = tabla_quiz_navigation.getChildAt(0) as LinearLayout
                for (i in 0 until tabStrip.childCount) {
                    tabStrip.getChildAt(i).setOnTouchListener { _, _ -> true }
                }
            }
        })

====

        // quiz_fragment.xml
        <android.support.design.widget.TabLayout
            android:id="@+id/tabla_quiz_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>

该方法在调用时间上略有不同,您只需设置选项卡项,在所有ViewPager片段添加后禁用其单击即可。
如果有人有更好的方法,请编辑我的答案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接