如何让listview的header不消耗触摸事件

how to make header of listview not to consume the touch event

我想要的

我想让listView的header不消费touch事件

问题

我有一个FrameLayout.In吧,有一个mapview和一个listview。列表视图有一个透明的 header 视图,以便显示更多的地图视图。现在 header 视图响应触摸事件,但我想将这些事件分派给地图视图。怎么办?

我试过了

都失败了,有什么建议吗?

您是否尝试在膨胀 header 视图或 this 时将 onClickListener 设置为 null?

我在SO中尝试了很多解决方案,但都不是work.Finally,我重写了dispatchTouchEvent()函数,它works.Complete版本已经结束gist ,关键代码在这里。

@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);
    }
}