0

Here I have a TabLayout and inside that I have ViewPager where are 4 tabs and all tabs are static. I needed a blank space in center of TabLayout so I added one more tab in TabLayout, now I want to disable it, it should not get any clicked, tap or selector should not come on that particular tab as it is showing in image as

enter image description here

I'm adding custom view this tab as

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

I tried to disable clicks etc on this tab as

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

but it didn't work for me. Can anyone give me a right way to solve this issue?

Bradley Wilson
  • 1,197
  • 1
  • 13
  • 26
Anshul Tyagi
  • 2,076
  • 4
  • 34
  • 65

1 Answers1

0

try this :

    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();
  }
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62