How to prevent the parent ViewPager from scrolling when the child of the ViewPager is in the last element?

I have a nested ViewPager that works brilliantly. The only problem is that after the child of the ViewPager is in the last element, and I scroll further, the parent scroll of the ViewPager scrolls. I do not want this behavior.

How do I achieve this?

Here is an example, in my Activity_main.xml I have a parent ViewPager that hosts 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 that is a child of the parent viewPager in 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"/> 

Everything works perfectly. But when the child viewPager reaches its last element, and I scroll more, the parent viewPager moves to the next page. I do not want this. I tried to do this by grabbing the touch of the parent ViewPager before, and that didn't work. I assume that I am doing it wrong? (Yes, I changed the tags in 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); } 
+3
source share
1 answer

First create a CustomViewPager to change its state using swipeable, as shown 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; } } 

The second change to your view pager that you want to disable, drag it into your CustomViewPager

Get viewing pagers:

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

Add onTouchListener to 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 the current position of your viewpager is:

  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 click in your MotionEvents if pos == yourAdaptersItemCount-1 and disable or enable your customViewPager.

Good luck.

+5
source

All Articles