我创建了一个自定义 View ,您可以在其中用手指绘制路径。它扩展了 View 类。
当我在 ListView 中使用它作为它的项目之一时,如果用户触摸自定义 View ,ListView 就会滚动。我怎样才能防止这种情况发生?
我想我需要以某种方式关注我的自定义 View 。但我不知道怎么办。
更新:
我找到了可能的解决方案。在我的自定义 View 的 onTouchEvent(Motion event)
方法中,我放置了 getParent().requestDisallowInterceptTouchEvent(true);
。
如果没有这个,当用户触摸自定义 View 时事件队列看起来像这样:
- MotionEvent.ACTION_DOWN
- MotionEvent.ACTION_MOVE
- MotionEvent.ACTION_MOVE
- MotionEvent.ACTION_CANCEL
当我收到带有代码 MotionEvent.ACTION_CANCEL
的 MotionEvent
时,ListView
开始滚动。
最佳答案
在 child 的 onTouch(View, MotionEvent)
方法中设置 requestDisallowInterceptTouchEvent(true)
有效:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup item = (ViewGroup) inflater.inflate(R.layout.list_item, null);
Button button = (Button)item.findViewById(R.id.list_item_btn);
button.setText("button " + position);
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
listView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
return item;
}
关于android - 不要在自定义 View 中滚动 ListView 被触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19172370/