【问题标题】:Disable ListView Scrolling when swiping ViewPager滑动 ViewPager 时禁用 ListView 滚动
【发布时间】:2013-07-25 08:53:00
【问题描述】:

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

谢谢。

最后编辑

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

这是我的自定义 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);

    }
}

【问题讨论】:

标签: android android-listview android-viewpager


【解决方案1】:

是的,有。创建另一个从 ListView 扩展的 customListView 类,并像这样覆盖它的 dispatchTouchEvent 事件处理程序:

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

然后改用这个 customListView

【讨论】:

  • 我给了你一个禁用滚动的例子,但显然我还不够清楚。在那个被覆盖的方法中,您应该检测手势并在它是 SWIPE 时禁用滚动。因为代码在下面的地址中可用,所以我没有复制它。看这个地址:stackoverflow.com/questions/2646028/…
  • 我已经更新了我的帖子,我按照您的指示,终于设法实现了所需的功能。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 2015-04-14
相关资源
最近更新 更多