如何使listview的标题不消耗touch事件 [英] how to make header of listview not to consume the touch event
问题描述
我想要的
我希望listView的标题不占用触摸事件
I want the header of listView not to consume the touch event
问题
我有一个FrameLayout,其中有一个mapview和一个listview.listview具有透明的标题视图,以显示更多的mapview.现在标题视图响应了触摸事件,但是我想将这些事件分派到mapview.怎么办?
I have a FrameLayout.In it, there are a mapview and a listview. The listview has a transparent header view in order to show more of the mapview. Now the header view response to the touch events, but I want dispatch these events to the mapview. How to do?
我已经尝试
-
headerView.setEnabled(false)
-
headerView.setClickable(false)
-
mListView.addHeaderView(headerView,null,false)
所有人都失败了,有什么建议吗?
All of them failed, any advice?
推荐答案
我在SO中尝试了很多解决方案,但是它们不起作用.最后,我重写了 dispatchTouchEvent()
函数,它完全版本已超过要点,并且关键代码在这里.
I try a lot of solutions in SO, but they are not work.Finally, I override the dispatchTouchEvent()
function, it works.Complete version is over gist
,and key code is in here.
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
if(mHeaderView == null) return super.dispatchTouchEvent(motionEvent);
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
//if touch header not to consume the event
Rect rect = new Rect((int) mHeaderView.getX(), (int) mHeaderView.getY(), mHeaderView.getRight(), mHeaderView.getBottom());
if(rect.contains((int)motionEvent.getX(), (int)motionEvent.getY())){
isDownEventConsumed = false;
return isDownEventConsumed;
}else {
isDownEventConsumed = true;
return super.dispatchTouchEvent(motionEvent);
}
}else{
//if touch event not consumed, then move/up event should be the same
if(!isDownEventConsumed)return isDownEventConsumed;
return super.dispatchTouchEvent(motionEvent);
}
}
这篇关于如何使listview的标题不消耗touch事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!