禁用listview中的滚动

Luk*_*kap 42 android listview scroll listener

我有一个列表视图,根据一些逻辑,我想临时禁用滚动.view.setOnScrollListener(NULL); 没有帮助我我想我需要写一些代码,有人可以给我一个文件或一些片段吗?

谢谢

Sur*_*jid 44

没有创建新的自定义ListView的另一个选项是附加onTouchListener到ListView,onTouch()如果运动事件操作是,则在回调中返回true ACTION_MOVE.

listView.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 如果ListView包含任何按钮或甚至拉动刷新,这**都不会全部工作**.它只是**不起作用**,如果用户碰巧滚动其中一个按钮 - 它滚动.它根本不可用. (7认同)

Poi*_*ull 42

在CustomListView中:

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

然后ListView将对点击作出反应,但不会改变滚动位置.

  • 看起来这个解决方案只是部分解决方案,并且在连接了外接键盘或BT/USB键盘的任何设备上都会失败. (5认同)
  • 不幸的是,这也是行不通的.如果您滑动按钮或任何其他内容,您仍然可以获得滚动效果. (4认同)

小智 29

使用listview.setEnabled(false)禁用listview滚动

注意:这也会禁用行选择

  • 为我工作,我不知道为什么其他人都采取这样复杂的路线. (7认同)
  • 这不起作用,因为它还禁用列表视图行选择 (5认同)

Moh*_*san 8

做你的 CustomListView

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
  if(needToStop){
    return false;}
    return super.onInterceptTouchEvent(ev); 
}
Run Code Online (Sandbox Code Playgroud)

false孩子的将处理触摸事件,确保你把你的if condition检查需要滚动与否.


Dan*_*pov 8

如果您有一个绑定到列表项的事件,则使用任何这些解决方案拖动列表仍将触发该事件.您希望使用以下方法来考虑用户期望通过拖离所选项目来取消事件(改编自Pointer Null的答案):

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
            super.dispatchTouchEvent(ev);
        } else {
            // Clear pressed state, cancel the action
            setPressed(false);
            invalidate();
            return true;
        }
    }

    return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)

提供完整的自定义视图类:https://gist.github.com/danosipov/6498490


Mau*_*eri 5

对我来说最好的答案是丹·奥西波夫的一个人.我会像这样改进它.(触发CANCEL动作事件而不是手动擦除按下状态).

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) != mPosition) {
            // Clear pressed state, cancel the action
            ev.setAction(MotionEvent.ACTION_CANCEL);
        }
    }

    return super.dispatchTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)