Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 get almost all Action.Move events when touching and sliding along X-axis. It seems that viewpager has a onInterceptTouchEvent which returns true.

Question

Is it real to override viewpager's behavior to make it and my layout work together? So the perfect situation is layout intercepts all onTouch events on it and viewpager manages the rest of onTouch events. Thanks!

like image 755
Oleksii Malovanyi Avatar asked Nov 14 '11 13:11

Oleksii Malovanyi


3 Answers

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;
    }
}
like image 106
alex.zherdev Avatar answered Oct 20 '22 08:10

alex.zherdev


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);
    }
}
like image 44
Kuffs Avatar answered Oct 20 '22 07:10

Kuffs


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!

like image 34
GeekLei Avatar answered Oct 20 '22 08:10

GeekLei