Ninja
Ninja

Reputation: 2568

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

All of them failed, any advice?

Upvotes: 2

Views: 235

Answers (2)

Ninja
Ninja

Reputation: 2568

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

Upvotes: 3

faranjit
faranjit

Reputation: 1637

Did you try to set onClickListener null while inflate header view or this?

Upvotes: 0

Related Questions