android-禁用在listview中滚动
我有一个列表视图,根据某些逻辑,我想暂时禁用滚动。view.setOnScrollListener(null); 我想我需要写一些代码对我没有帮助,有人可以给我一个历史记录或一些摘要吗?
谢谢
10个解决方案
44 votes
不创建新的自定义ListView的另一个选项是将onTouchListener附加到ListView上,如果运动事件动作为ACTION_MOVE,则在onTouch()回调中返回true。
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
Surya Wijaya Madjid answered 2020-08-02T20:02:30Z
42 votes
在您的自定义ListView中:
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(ev.getAction()==MotionEvent.ACTION_MOVE)
return true;
return super.dispatchTouchEvent(ev);
}
然后,ListView将对单击做出反应,但不会更改滚动位置。
Pointer Null answered 2020-08-02T20:02:09Z
28 votes
使用listview.setEnabled(false)禁用列表视图滚动
注意:这也会禁用行选择
Nikita answered 2020-08-02T20:02:54Z
8 votes
使您的CustomListView
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(needToStop){
return false;}
return super.onInterceptTouchEvent(ev);
}
在false上,孩子们将处理触摸事件,请确保您将if condition放进去,以检查是否需要滚动。
Mohammad Ersan answered 2020-08-02T20:03:18Z
8 votes
如果您有一个事件绑定到列表项,则使用任何这些解决方案拖动列表仍将触发该事件。 您想要使用以下方法来解决用户期望通过从所选项目上拖动(根据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 == MotionEven