Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
}
like image 915
Adrian Olar Avatar asked Jul 25 '13 08:07

Adrian Olar


People also ask

How do I stop ViewPager from scrolling?

A simple solution is to create your own subclass of ViewPager that has a private boolean flag, isPagingEnabled . Then override the onTouchEvent and onInterceptTouchEvent methods. If isPagingEnabled equals true invoke the super method, otherwise return .

How do I turn off swipe in ViewPager Kotlin?

There is no built in way to disable swiping between pages of a ViewPager, what's required is an extension of ViewPager that overrides onTouchEvent and onInterceptTouchEvent to prevent the swiping action. To make it more generalised we can add a method setSwipePagingEnabled to enable/disable swiping between pages.


1 Answers

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

like image 120
Masoud Dadashi Avatar answered Oct 19 '22 23:10

Masoud Dadashi