Adrian Olar
Adrian Olar

Reputation: 2903

Disable ListView Scrolling when swiping ViewPager

is there a way to lock the vertical scrolling of a ListView while scrolling an item which is a ViewPager? Or perhaps change the horizontal scrolling sensitivity of the ViewPager?

Thanks.

LAST EDIT

Here is my updated solution. Thanks for your replies Masoud Dadashi, your comments finally made me came up with a solution to my problem.

Here is my custom ListView class:

public class FolderListView extends ListView {

    private float xDistance, yDistance, lastX, lastY;

    // If built programmatically
    public FolderListView(Context context) {
        super(context);
        // init();
    }

    // This example uses this method since being built from XML
    public FolderListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // init();
    }

    // Build from XML layout
    public FolderListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // init();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if (xDistance > yDistance)
                return false;
        }

        return super.onInterceptTouchEvent(ev);

    }
}

Upvotes: 14

Views: 9281

Answers (1)

Masoud Dadashi
Masoud Dadashi

Reputation: 1074

yes there is. create another customListView class extended from ListView and override its dispatchTouchEvent event handler like this:

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
   if(ev.getAction()==MotionEvent.ACTION_MOVE)
      return true;
   return super.dispatchTouchEvent(ev);
}

then use this customListView instead

Upvotes: 7

Related Questions