Disable ListView Scrolling when swiping ViewPager

强颜欢笑 提交于 2019-12-13 12:53:08

问题


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

    }
}

回答1:


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



来源:https://stackoverflow.com/questions/17853312/disable-listview-scrolling-when-swiping-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!