在TabLayout中禁用一个选项卡?

11

我正在开发一个应用程序,需要用到选项卡。因此,我最终选择了选项卡布局,并包含了几个选项卡。代码如下:


private void setupNavTab() {
    int[][] states = new int[][]{
            new int[]{android.R.attr.state_selected},
            new int[]{-android.R.attr.state_selected},
            new int[]{-android.R.attr.state_enabled}
    };

    int[] colors = new int[]{
            ContextCompat.getColor(getActivity(), R.color.cricut_selected_green),
            ContextCompat.getColor(getActivity(), R.color.nav_bar_unselected_content),
            ContextCompat.getColor(getActivity(), R.color.edit_button_text_color_inactive)
    };
    ColorStateList cslist = new ColorStateList(states, colors);

    if (tabs != null) {
        tabs.removeAllTabs();
        tabs.setTabTextColors(cslist);

        firstTab = tabs.newTab().setTag(TAB_FIRST);
        View customFirstTabView = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_layout, null, false);
        firstTabView = (TextView) customFirstTabView.findViewById(R.id.textContainer);

        firstTabView.setText(R.string.first);
        firstTabView.setTextColor(cslist);
        firstTab.setCustomView(customFirstTabView);

        secondTab = tabs.newTab().setTag(TAB_SECOND);
        View customSecondView = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_layout, null, false);
        secondTabView = (TextView) customSecondView.findViewById(R.id.textContainer);

        secondTabView.setText(R.string.second);
        secondTabView.setTextColor(cslist);
        secondTab.setCustomView(customSecondView);

        thirdTab = tabs.newTab().setTag(TAB_THIRD);
        View customThirdTabView
                = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_layout, null, false);
        thirdTabView = (TextView) customThirdTabView.findViewById(R.id.textContainer);

        thirdTabView.setText(R.string.third);
        thirdTabView.setTextColor(cslist);
        thirdTab.setCustomView(customThirdTabView);

        tabs.addTab(firstTab, true);
        tabs.addTab(secondTab, false);
        tabs.addTab(thirdTab, false);
    }
}

我没有使用ViewPager。 在这里,用户应该在完成第一个选项卡和第二个选项卡中的必要步骤之前无法访问第三个选项卡。是否有一种方法在用户完成这些步骤之前禁用第三个选项卡?



1
查看这个链接是否有帮助:https://dev59.com/CFwZ5IYBdhLWcg3wPOJM#32104992 - Harin
谢谢您的评论。那个方法有效。请将其作为答案发布,以便我可以接受它。 - SarojMjn
嘿,Saroj,你禁用了特定位置的单个选项卡吗?我尝试了相同的代码,但没有成功。你可以告诉我吗? - Anshul Tyagi
你看了上面提到的链接吗?我相信你可以使用pat8719所接受的答案正下方的那个答案。 - SarojMjn
3个回答

7
您可以访问选项卡的视图属性。
tabs.getTabAt(index).view.setClickable(true); //enable clicking
tabs.getTabAt(index).view.setClickable(false); //disable clicking

5
使用此代码以禁用所有选项卡项目:

disable all

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

使用以下代码禁用它们中的一个:(例如禁用项目2)

LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
    tabStrip.getChildAt(2).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

请注意,第二个条目必须存在

感谢来自harry的评论中提到的答案


2

我仔细查找了一下。在MainActivity中,这里声明了tabLayout为public,在此片段中,当音频录制正在进行时,我将所有选项卡设置为禁用。

TabLayout tabLayout = MainActivity.tabLayout;
for (int i = 0; i < 3; i++){
    tabLayout.getTabAt(i).view.setEnabled(b);
}

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