1

I'm trying to make a single activity with a modular section of its screen that houses a variety of non-sequential fragments.

However, using ViewPager allows the user to scroll through all of these fragments at will. I'd like to set it up so that the fragments can only be seen and accessed if the user clicks a button in the fixed section of the activity, or in another one of the fragments. Is this possible?

What would be the best way to get this effect?

andrewedgar
  • 797
  • 1
  • 12
  • 46

1 Answers1

2

Yes, you can subclass ViewPager to intercept user inputs to disable the scroll. Then you can control the ViewPager with your buttons directly.

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

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

Taken from https://stackoverflow.com/a/7814054/3106174

advice
  • 5,778
  • 10
  • 33
  • 60