动态禁用Android TabLayout中单个标签的点击/滑动/点击

时间:2017-01-05 09:59:05

标签: android android-viewpager android-tablayout

这里我有一个TabLayout,里面有ViewPager,其中有4个标签,所有标签都是静态的。我在TabLayout的中心需要一个空格,所以我在TabLayout中添加了一个标签,现在我要禁用它,它不应该被点击,点按或选择器不应该出现在该特定标签上因为它在图像中显示为

enter image description here

我将此标签添加为自定义视图

  for (int i = 0; i < mPagerAdapter.getCount(); i++) {
        View customView = mPagerAdapter.getCustomeView(getActivity(), i);
        mTabLayout.getTabAt(i).setCustomView(customView);
    }

我尝试在此标签上停用点按等

 LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(2));
 tabStrip.setEnabled(false);

但它对我没用。任何人都可以给我一个正确的方法来解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

    tabLayout.setupWithViewPager(customViewPager);
customViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i <tabLayout.getTabCount(); i++){
            updateTab(tabLayout.getTabAt(i), position == i);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});



private void updateTab(TabLayout.Tab tab, boolean isSelected){
  Method method = null;
  try {
      method = TabLayout.Tab.class.getDeclaredMethod("getCustomView", null);
      method.setAccessible(true);

      View tabview = (View) method.invoke(tab, null);
      //disable tabview here for specific tab
     }

      tab.setCustomView(tabview);
  } catch (Exception e) {
      e.printStackTrace();
  }
}