Search code examples
androidlistviewtouch-event

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


What I want

I want the header of listView not to consume the touch event

Problem

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?

I have tried

  • headerView.setEnabled(false)
  • headerView.setClickable(false)
  • mListView.addHeaderView(headerView, null, false)

All of them failed, any advice?


Solution

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