14

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);

    }
}
Adrian Olar
  • 2,883
  • 4
  • 35
  • 63
  • Look at this post http://stackoverflow.com/questions/7900860/viewpager-in-a-listview-how-to-lock-the-scrolling-axis – An-droid Jul 25 '13 at 09:04

1 Answers1

7

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

Masoud Dadashi
  • 1,044
  • 10
  • 11
  • 1
    I gave you an example of disabling scroll but apparently I wan't clear enough. In that overridden method you should detect the gesture and disable scrolling if it is SWIPE. since code is available in the address below I am not duplicating it. look at this address: http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling – Masoud Dadashi Jul 25 '13 at 10:52
  • I have updated my post, i followed your instructions and finally managed to achieve the desired functionality. Thank you so much! – Adrian Olar Jul 25 '13 at 11:12