2016-08-03 58 views
2

我想如何让列表视图的头不消耗触摸事件

我想要的ListView头不消耗触摸事件

问题

我有什么在FrameLayout中,有一个mapview和一个listview。该listview有一个透明的标题视图,以显示更多的mapview。现在,标题视图响应触摸事件,但我想将这些事件分派给mapview。怎么做?

我已经试过

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

他们都失败了,有什么建议?

+0

我能想到的防止标题消耗触摸事件的唯一方法是将其可见性设置为隐形。 – Max

+0

想你@安德斯。我试过你的解决方案,但不工作... – Ninja

回答

2

我尝试了很多解决方案的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); 
    } 
} 
+0

这也适用于我! –

+0

@PranoyC很高兴看到代码可以帮助你 – Ninja

0

您是否尝试在充气标题视图或this时设置onClickListener空?

+0

我试过了,但没有工作。最后我重写了'dispatchTouchEvent()'函数,它的工作原理〜 – Ninja