[英]Do not scroll ListView when cutsom view is touched
I've created a custom view on which you can draw a path with your finger. 我创建了一个自定义视图,您可以用手指绘制路径。 It extends View class. 它扩展了View类。
When I use it inside a ListView as one of its items if a user touches custom view the ListView is scrolled. 当我在ListView中使用它作为其项目之一时,如果用户触摸自定义视图,则滚动ListView。 How can I prevent this from happening? 我怎样才能防止这种情况发生?
I suppose I need to get focus somehow on my custom view. 我想我需要以某种方式在我的自定义视图上获得焦点。 But I don't know how. 但我不知道怎么做。
Update: 更新:
I found possible solution. 我发现可能的解决方案 In my custom view's onTouchEvent(Motion event)
method I've placed getParent().requestDisallowInterceptTouchEvent(true);
在我的自定义视图的onTouchEvent(Motion event)
方法中,我放置了getParent().requestDisallowInterceptTouchEvent(true);
. 。
Without this the event queue when user touches custom view looked like this: 如果没有这个,当用户触摸自定义视图时,事件队列如下所示:
When I receive MotionEvent
with code MotionEvent.ACTION_CANCEL
the ListView
starts to scroll. 当我使用代码MotionEvent.ACTION_CANCEL
接收MotionEvent
, ListView
开始滚动。
Setting requestDisallowInterceptTouchEvent(true)
within child's onTouch(View, MotionEvent)
method works: 在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;
}
I think u should also extend the ListView widget . 我想你也应该扩展ListView小部件。
As u say ,when u touch the custom view ,the listview will scroll. 正如您所说,当您触摸自定义视图时,列表视图将滚动。
when u touch the custom view, the touch event will dispatch along the view hierarchy , 当您触摸自定义视图时,触摸事件将沿视图层次结构分派,
I think listview may eat the touch event. 我认为listview可能会吃掉触摸事件。
so ur customview can not receive the touch event. 所以你的customview无法接收触摸事件。
if ur have not understand the dispatch of the touch event. 如果你不理解触摸事件的发送。
u can read this Managing Touch Events in a ViewGroup 你可以在ViewGroup中阅读这个管理触摸事件
at last. 最后。
I suggest u extend the ListView and override the onInterceptTouchEvent() method. 我建议你扩展ListView并覆盖onInterceptTouchEvent()方法。
u can decide if ur ListView want to handle the touch event in the onInterceptTouchEvent() . 你可以决定你的ListView是否想要处理onInterceptTouchEvent()中的触摸事件。
when u judge the touch should be handle by the drawPathView, 当你判断触摸应该由drawPathView处理时,
u can let the ListView dont intercept it. 你可以让ListView不要拦截它。
so ur drawPathView can get the toch event . 所以你的drawPathView可以得到toch事件。
and also the listView will not scroll 而且listView也不会滚动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.