Skip to content Skip to sidebar Skip to footer

Disable Click On Tablayout, When Setup With Viewpager

I have a ViewPager (3 items) with some icons. When on first item, the icon is selected, there is possible to swipe to the next item. Etc. After selected second icon, there is possi

Solution 1:

try this for api >24:

tabLayout.clearOnTabSelectedListeners();

or for <24:

tabLayout.setupWithViewPager(viewPager);

LinearLayout tabStrip = ((LinearLayout)tabLayout.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;
        }
    });
}

Solution 2:

You can extend the TabLayout class and override the method 'setupWithViewPager'. You need to call the super method to populate the tabs. Then this will iterate with the tabs and disable clicks:

@Override
public void setupWithViewPager(@Nullable ViewPager viewPager) {
    super.setupWithViewPager(viewPager);

    for (int i = 0; i < getTabCount(); i++) {
        Tab tab = getTabAt(i);
        if (tab != null) {
            tab.view.setClickable(false);
        }
    }
}

Post a Comment for "Disable Click On Tablayout, When Setup With Viewpager"