2016-10-16 45 views
0

我有一個自定義類標題,這是完全透明的列表視圖。在列表視圖後面,我有一個通過透明標題顯示的mapview。android listview header通過觸摸來查看後面

我想使這個工作,以便如果用戶滾動我的觸摸列表視圖中的任何行,然後列表視圖滾動。但是,如果用戶正在觸摸列表視圖的頂部透明標題部分,則列表視圖不應攔截觸摸,而應將其傳遞到後面的mapview(它允許用戶在地圖視圖上平移/縮放)。

目前我無法做到這一點,因爲listview一直在竊取觸摸。任何幫助,將不勝感激。

回答

0

nvm,我找到了類似問題的解決方案。不知道爲什麼在發佈這個問題之前我沒有找到它,但之後,SO在右邊的相關問題中向我展示了它。

解決方案: how to make header of listview not to consume the touch event

使用自定義的ListView類:

package xxx.xxx.xxxxxx; 

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ListView; 

public class HeaderUntouchableListView extends ListView { 
    private View mHeaderView; 
    private boolean isDownEventConsumed; 

    public HeaderUntouchableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void addHeaderView(View v) { 
     super.addHeaderView(v); 
     this.mHeaderView = v; 
    } 

    @Override 
    public void addHeaderView(View v, Object data, boolean isSelectable) { 
     super.addHeaderView(v, data, isSelectable); 
     this.mHeaderView = v; 
    } 

    /** 
    * list header should not consume the event, and list item should consume the event 
    * consumed here is replaced with super.dispatchTouchEvent(motionEvent) 
    * @param motionEvent 
    * @return is event consumed 
    */ 
    @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); 
     } 
    } 
}