要禁用TabLayout中的其中一个标签吗?

问题描述 投票:5回答:1

我需要我正在开发的应用程序的标签。所以我最后以tablayout结束,它包含几个选项卡。代码如下:


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。在这里,第三个选项卡应该是不可访问的,直到用户完成了firstTab和secondTab中的必要步骤。在用户完成这些步骤之前,有没有办法禁用thirdTab?

android android-tablayout androiddesignsupport
1个回答
2
投票

将此用于全部禁用选项卡项目:

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

请注意项目2必须存在

感谢harry在评论中引用此answer

© www.soinside.com 2019 - 2024. All rights reserved.