4

I have a nested ViewPager that works brilliantly. The only issue is, once the child ViewPager is at the last item and I scroll further, the parent ViewPager scrolls. I do not want this behaviour.

How do I achieve this?

Here is an example, in my Activity_main.xml I have the parent ViewPager, which houses three fragment pages.

 <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Inside one of the fragment layouts, fragment_categories.xml, I have another ViewPager, which is a child to the parent viewPager in the Activity_main.xml,

<android.support.v4.view.ViewPager
    android:id="@+id/mViewPager"
    android:layout_width="match_parent"
    android:layout_height="@dimen/offer_height"
    android:layout_marginTop="2dp"
    android:overScrollMode="never"/>

It all works fine. But when the child viewPager reaches its last item, and I scroll more, the parent viewPager moves to the next page. I do not want this. I tried doing this by intercepting touch on the parent ViewPager before, and that didn't work. I'm guessing I'm doing it wrong? (Yes, I changed tags in the xml to com.project.CustomViewPager and tried this.)

CustomViewPager.java:

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

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if(v != this && v instanceof ViewPager) {
            return false;
        }

        return super.canScroll(v, checkV, dx, x, y);
    }
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
SergeantPeauts
  • 372
  • 1
  • 2
  • 15

1 Answers1

5

First create a CustomViewPager to change it's swipeable state like below:

public class CustomViewPager extends ViewPager {
    private boolean canScroll = true;
    public CustomViewPager(Context context) {
        super(context);
    }
    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setCanScroll(boolean canScroll) {
        this.canScroll = canScroll;
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return canScroll && super.onTouchEvent(ev);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return canScroll && super.onInterceptTouchEvent(ev);
    }

    public boolean isCanScroll() {
        return canScroll;
    }
}

Second change your view pager which you want to disable it's swipe propery to your CustomViewPager

Get your view pagers:

CustomViewPager myCustomViewPager = (CustomViewPager) findViewById(R.id.my_custom_pager);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

Add an onTouchListener to your ViewPager:

viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        myCustomViewPager.setCanScroll(false);
                        break;

                    case MotionEvent.ACTION_UP:
                        myCustomViewPager.setCanScroll(true);
                        break;
                }
                return false;
            }
        });

If you really need to check if your viewpager's current position:

    int pos = 0;
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                   pos = position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

And you can chek in your MotionEvents if pos == yourAdaptersItemCount-1 and disable or enable your customViewPager.

Good luck.

savepopulation
  • 11,736
  • 4
  • 55
  • 80