ScrollView inside ViewPager: swipe not working

后端 未结 3 1405
一个人的身影
一个人的身影 2021-01-06 21:44

I have ViewPager with three fragments and one of them is FrameLayout with ScrollView inside:



        
相关标签:
3条回答
  • 2021-01-06 22:01

    Ok, I found the solution with help of @yedidyak. I wrote my custom ScrollView:

    public class CustomScrollView extends ScrollView {
    
    float touchX = 0;
    float touchY = 0;
    
    ViewPager parentPager;
    
    public void setParentPager(ViewPager parentPager) {
        this.parentPager = parentPager;
    }
    
    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomScrollView(Context context) {
        super(context);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    
        switch (ev.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                touchX = ev.getX();
                touchY = ev.getY();
                return super.onTouchEvent(ev);
            case MotionEvent.ACTION_MOVE:
                if(Math.abs(touchX-ev.getX())<40){
                    return super.onTouchEvent(ev);
                }else{
                    if (parentPager==null) {
                        return false;
                    } else {
                        return parentPager.onTouchEvent(ev);
                    }
                }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                touchX=0;
                touchY=0;
                break;
        }
        return super.onTouchEvent(ev);
    }
    }
    

    then in the fragment I put the viewpager to this view and it works perfectly

    0 讨论(0)
  • 2021-01-06 22:05

    For Future Readers living in +2019

    use ViewPager2 to avoid this problem. you can find good example of ViewPager2 implementation at this topic.

    0 讨论(0)
  • 2021-01-06 22:07

    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.

    0 讨论(0)
提交回复
热议问题