2016-08-03 61 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