当滑动ViewPager时禁用ListView的滚动功能

14

有没有办法在滚动ViewPager中的项目时锁定ListView的垂直滚动?或者改变ViewPager的水平滚动灵敏度?

谢谢。

最后编辑

这是我的更新解决方案。感谢Masoud Dadashi的回复,您的评论最终让我想出了解决我的问题的方法。

这是我的自定义ListView类:

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

    }
}

查看此帖子 https://dev59.com/jmsz5IYBdhLWcg3wcXTF - An-droid
1个回答

7
有的。创建另一个自定义的ListView类,扩展自ListView,并覆盖它的dispatchTouchEvent事件处理程序,就像这样:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
   if(ev.getAction()==MotionEvent.ACTION_MOVE)
      return true;
   return super.dispatchTouchEvent(ev);
}

然后使用这个自定义ListView。

1
我给了你一个禁用滚动的例子,但显然我没有表达清楚。在那个重写的方法中,你应该检测手势,并且如果是SWIPE,则禁用滚动。由于代码可以在下面的地址中找到,我不会重复它。看一下这个地址:https://dev59.com/XHE85IYBdhLWcg3wtV71 - Masoud Dadashi
我已经更新了我的帖子,我按照你的指示并最终成功实现了所需的功能。非常感谢你! - Adrian Olar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接