Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make tabs in SlidingTabLayout not slide

I recently made an app using SlidingTabLayout with two tabs. I referred this link

Sliding Tabs

However I had to modify it slightly. I had to add a button which locks the sliding of the tabs. And unlock it when it is clicked again. So I just can't get the tabs to not slide.

I checked out this question Disable swiping between tabs. But he is using some other library to do it and it's no longer supported. I'm using the default one. And in that question the CustomViewPager extends android.support.v4.view.ViewPager. In my project ViewPagerAdapter extends FragmentStatePagerAdapter.

Any help would be very useful. Thank you.

like image 683
MVK059 Avatar asked Dec 22 '15 10:12

MVK059


People also ask

How do I disable TabLayout click?

the method has slightly different on the time of its invocation, you just have to setup your tabitem to disable its click after all viewpager fragment already added.

How do I set ViewPager to TabLayout?

Android ViewPager ViewPager with TabLayout A TabLayout can be used for easier navigation. You can set the tabs for each fragment in your adapter by using TabLayout. newTab() method but there is another more convenient and easier method for this task which is TabLayout. setupWithViewPager() .

Can we use TabLayout without ViewPager in Android?

It is possible to use a TabLayout without a ViewPager by using a TabLayout. OnTabSelectedListener . For navigation within an Activity , manually populate the UI based on the tab selected.


1 Answers

You can just make a custom ViewPager which extends the ViewPager and set a method that disables and enables the swiping.

You can do that by adding a class like the one below to your code. Then instead of using ViewPager just use CustomViewPager in your code:

public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
             return super.onTouchEvent(event);
        }
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }
        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

You can disable/enable swiping by calling: setPagingEnabled(boolean enabled).

like image 126
Irina Avram Avatar answered Oct 08 '22 15:10

Irina Avram