Make tabs in SlidingTabLayout not slide
Asked Answered
W

1

6

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.

Warbler answered 22/12, 2015 at 10:8 Comment(6)
Can you just post your code.Disclaimer
Post your modified codeWhat
It's the same. I haven't modified it. If i got to know the logic of not sliding the tabs I can proceed further. Thank youWarbler
So you want lock changing tabs. If I click on button I can't change tab, but then I click again, the feature is unlocked, right?Basically
Yes, I want to do that.Warbler
Possible duplicate of Disable swiping between tabsBaseless
G
1

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).

Glinys answered 22/12, 2015 at 10:13 Comment(7)
Have you checked out the ViewPager of the link I specified? Are you telling me to replace that fully?Warbler
The ViewPager is from the android support v4 library, and yes, I checked and saw that it was used in your code. So basically I am saying, add this class to your code and replace ViewPager with CustomViewPager where it is used. There actually are not so many references where you need to replace itGlinys
And how do I call this in MainActivity as the current one is using FragmentManagerWarbler
you never call it using the FragmentManager in the code you provided, you call the adapter that way. As for the custom ViewPager you just use it like this CustomViewPager pager = (CustomViewPager) findViewById(R.id.pager);Glinys
It's giving an error: android.support.v4.view.ViewPager cannot be cast to mvk.com.slidingtabs.CustomViewPager. Am I doing something wrong?Warbler
well your CustomViewPager should extend the ViewPager from mvk.com.slidingtabs not the one from the support library. Sorry, my bad, I thought you used that one.Glinys
Let us continue this discussion in chat.Warbler

© 2022 - 2024 — McMap. All rights reserved.