禁用tablayout中的一个标签?

时间:2016-02-29 06:06:32

标签: android android-tablayout androiddesignsupport

我需要制作我正在开发的应用程序的标签。所以我最终得到了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?

3 个答案:

答案 0 :(得分:0)

将此用于禁用所有标签项:

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

答案 1 :(得分:0)

您可以访问标签的视图属性

tabs.getTabAt(index).view.setClickable(true); //enable clicking
tabs.getTabAt(index).view.setClickable(false); //disable clicking

答案 2 :(得分:0)

我在上下寻找。这里的tabLayout在MainActivity中声明为公共,在此片段中,我将它们设置为在进行AudioRecording时全部禁用。

TabLayout tabLayout = MainActivity.tabLayout;

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