Android 如何使listview的标题不使用触摸事件
我想要什么 我希望listView的标题不使用触摸事件 问题 我有一个框架布局,里面有一个地图视图和一个列表视图。listview有一个透明的标题视图,以便显示更多的mapview。现在header视图响应触摸事件,但我希望将这些事件发送到mapview。怎么办 我已经试过了Android 如何使listview的标题不使用触摸事件,android,listview,touch-event,Android,Listview,Touch Event,我想要什么 我希望listView的标题不使用触摸事件 问题 我有一个框架布局,里面有一个地图视图和一个列表视图。listview有一个透明的标题视图,以便显示更多的mapview。现在header视图响应触摸事件,但我希望将这些事件发送到mapview。怎么办 我已经试过了 headerView.setEnabled(false) headerView.setClickable(false) mListView.addHeaderView(headerView,null,false) 所有
headerView.setEnabled(false)
headerView.setClickable(false)
mListView.addHeaderView(headerView,null,false)
所有这些都失败了,有什么建议吗?在充气标题视图时,您是否尝试将
onClickListener设置为null,或者?我在SO中尝试了很多解决方案,但都不起作用。最后,我重写了dispatchTouchEvent()
函数,它可以工作了。完整的版本已经结束
,关键代码在这里
@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);
}
}
我能想到的防止标题使用触摸事件的唯一方法是将其可见性设置为不可见。想想你@Anders。我尝试了你的解决方案,但没有成功…我尝试了,但没有成功。最后,我重写了dispatchTouchEvent()
函数,它可以工作~