Scrollview Inside Viewpager: Swipe Not Working
Solution 1:
Ok, I found the solution with help of @yedidyak. I wrote my custom ScrollView:
publicclassCustomScrollViewextendsScrollView {
floattouchX=0;
floattouchY=0;
ViewPager parentPager;
publicvoidsetParentPager(ViewPager parentPager) {
this.parentPager = parentPager;
}
publicCustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicCustomScrollView(Context context) {
super(context);
}
@OverridepublicbooleanonTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()){
case MotionEvent.ACTION_DOWN:
touchX = ev.getX();
touchY = ev.getY();
returnsuper.onTouchEvent(ev);
case MotionEvent.ACTION_MOVE:
if(Math.abs(touchX-ev.getX())<40){
returnsuper.onTouchEvent(ev);
}else{
if (parentPager==null) {
returnfalse;
} else {
return parentPager.onTouchEvent(ev);
}
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
touchX=0;
touchY=0;
break;
}
returnsuper.onTouchEvent(ev);
}
}
then in the fragment I put the viewpager to this view and it works perfectly
Solution 2:
The problem is that it isn't clear what scroll you want to happen, that of the ViewPager or that of the scrollview. If you really need a scroll-inside-a-scroll, then you need to override the OnTouchListener of the inner scrollview and add code that decides when to catch and use the touch, and when to pass it back to the parent views.
In this case, you can do something that tests if the swipe is up/down, and then keep the touch, otherwise if the swipe is sideways then passes it back.
Solution 3:
For Future Readers living in +2019
use ViewPager2 to avoid this problem. you can find good example of ViewPager2 implementation at this topic.
Post a Comment for "Scrollview Inside Viewpager: Swipe Not Working"