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.

Community
  • 1
  • 1
MVK059
  • 251
  • 3
  • 14

1 Answers1

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

Irina Avram
  • 1,319
  • 1
  • 15
  • 30
  • Have you checked out the ViewPager of the link I specified? Are you telling me to replace that fully? – MVK059 Dec 22 '15 at 10:31
  • 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 it – Irina Avram Dec 22 '15 at 10:34
  • And how do I call this in MainActivity as the current one is using FragmentManager – MVK059 Dec 22 '15 at 10:39
  • 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);` – Irina Avram Dec 22 '15 at 10:45
  • It's giving an error: android.support.v4.view.ViewPager cannot be cast to mvk.com.slidingtabs.CustomViewPager. Am I doing something wrong? – MVK059 Dec 22 '15 at 10:50
  • 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. – Irina Avram Dec 22 '15 at 10:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98678/discussion-between-mvk059-and-irina-avram). – MVK059 Dec 22 '15 at 10:54