ViewPager intercepts all x-axis onTouch events. How to disable?

后端 未结 4 2019
自闭症患者
自闭症患者 2020-11-29 04:22

Scope

There is a viewpager of two fragments. One of those fragments has a layout witch listens to onTouch changes at X-axis.

Problem

Layout doesn\'t

相关标签:
4条回答
  • 2020-11-29 04:58

    You are right, I believe every scrolling container intercepts touch events, but you can prevent it. You can put a touch listener on your layout:

    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE: 
            pager.requestDisallowInterceptTouchEvent(true);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            pager.requestDisallowInterceptTouchEvent(false);
            break;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 04:58

    Similar situation (but not using a ViewPager), putting this in the view that needed the touch event worked for me. Add checks for MotionEvents other than ACTION_MOVE if applicable to your use case.

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            this.getParent().requestDisallowInterceptTouchEvent(true);
            return true;
        } else {
            return super.onTouchEvent(event);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:04

    neutrino was right!

    getParent().requestDisallowInterceptTouchEvent(true);
    

    once the viewpager access the touchEvent Intercept,the child view in it can got the event. enter image description here


    I use a FrameLayout in viewpager to got the DrawerLayout Effect(I need it not match the height of screen,so I can't use drawerlayout or navigation drawer).
    It really helps!

    0 讨论(0)
  • 2020-11-29 05:07

    I had a similar problem.

    In my case I was setting a OnTouchListener on ViewPager but it wasn't receiving touch events when the children that received the touch had onClick set.

    What I did was extend the ViewPager class and call my click listener inside the method onInterceptTouchEvent(boolean) and it worked fine. Just be careful not to intercept wrong events.

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