ScrollView inside ViewPager: swipe not working

后端 未结 3 1407
一个人的身影
一个人的身影 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

提交回复
热议问题