I am using VerticalViewPager and two fragments in it.
One of this fragment contains RecyclerView with just regular vertical list. The problem is that I need this list to get to end and then change page at view pager and now view pager always try to intercept RecyclerView scroll.
My VerticalViewPager class:
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setPageTransformer(true, new VerticalPageTransformer());
setOverScrollMode(OVER_SCROLL_NEVER);
requestDisallowInterceptTouchEvent(false);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View view, float position) {
if (position < -1) {
view.setAlpha(0);
} else if (position <= 1) {
view.setAlpha(1);
view.setTranslationX(view.getWidth() * -position);
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else {
view.setAlpha(0);
}
}
}
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev);
return intercepted;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
}
And my layout tree:
- -Fragment
- --ViewPager
- ---Fragment_A
- ----RecyclerView
- ---Fragment_B
I already tried different combinations of
.setNestedScrollingEnabled(false/true);
.requestDisallowInterceptTouchEvent(false/true);
Tried to wrap RecyclerView with NestedScrollView, but scroll is still messy and laggy, ViewPager change its page at the list beginning or in the middle.
Thanks for your help!
I figure it with writing custom OnTouchListener for my RecyclerView:
and changed my VerticalViewPager:
This repo helped me a little bit.