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

假装没事ソ 提交于 2019-12-10 23:37:00

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/38735963/how-to-make-header-of-listview-not-to-consume-the-touch-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!