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. How can I prevent this from happening?
当我在ListView中使用它作为其项目之一时,如果用户触摸自定义视图,则滚动ListView。我怎样才能防止这种情况发生?
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事件)方法中,我放置了getParent()。requestDisallowInterceptTouchEvent(true);.
Without this the event queue when user touches custom view looked like this:
如果没有这个,当用户触摸自定义视图时,事件队列如下所示:
- MotionEvent.ACTION_DOWN
- MotionEvent.ACTION_MOVE
- MotionEvent.ACTION_MOVE
- MotionEvent.ACTION_CANCEL
When I receive MotionEvent
with code MotionEvent.ACTION_CANCEL
the ListView
starts to scroll.
当我使用代码MotionEvent.ACTION_CANCEL接收MotionEvent时,ListView开始滚动。
2 个解决方案
#1
2
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;
}
#2
0
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.
so ur customview can not receive the touch event.
if ur have not understand the dispatch of the touch event.
u can read this Managing Touch Events in a ViewGroup
正如您所说,当您触摸自定义视图时,列表视图将滚动。当你触摸自定义视图时,触摸事件将沿着视图层次结构调度,我认为listview可能会吃掉触摸事件。所以你的customview无法接收触摸事件。如果你不理解触摸事件的发送。你可以在ViewGroup中阅读这个管理触摸事件
at last.
I suggest u extend the ListView and override the onInterceptTouchEvent() method.
u can decide if ur ListView want to handle the touch event in the onInterceptTouchEvent().
when u judge the touch should be handle by the drawPathView,
u can let the ListView dont intercept it.
so ur drawPathView can get the toch event .
and also the listView will not scroll
最后。我建议你扩展ListView并覆盖onInterceptTouchEvent()方法。你可以决定你的ListView是否想要处理onInterceptTouchEvent()中的触摸事件。当你判断触摸应该由drawPathView处理时,你可以让ListView不要拦截它。所以你的drawPathView可以得到toch事件。而且listView也不会滚动
#1
2
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;
}
#2
0
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.
so ur customview can not receive the touch event.
if ur have not understand the dispatch of the touch event.
u can read this Managing Touch Events in a ViewGroup
正如您所说,当您触摸自定义视图时,列表视图将滚动。当你触摸自定义视图时,触摸事件将沿着视图层次结构调度,我认为listview可能会吃掉触摸事件。所以你的customview无法接收触摸事件。如果你不理解触摸事件的发送。你可以在ViewGroup中阅读这个管理触摸事件
at last.
I suggest u extend the ListView and override the onInterceptTouchEvent() method.
u can decide if ur ListView want to handle the touch event in the onInterceptTouchEvent().
when u judge the touch should be handle by the drawPathView,
u can let the ListView dont intercept it.
so ur drawPathView can get the toch event .
and also the listView will not scroll
最后。我建议你扩展ListView并覆盖onInterceptTouchEvent()方法。你可以决定你的ListView是否想要处理onInterceptTouchEvent()中的触摸事件。当你判断触摸应该由drawPathView处理时,你可以让ListView不要拦截它。所以你的drawPathView可以得到toch事件。而且listView也不会滚动