2015-10-07 53 views
-1

我有嵌套在ListView這樣的GridView無法設置OnItemClickListener上的ListView當它嵌套的GridView

enter image description here

ListView包含事件(行)和GridView用戶參加(行項目)。現在我想將OnItemClickListener附加到我的ListView,但是當我點擊一行時什麼也沒有發生。我試圖調試這個問題,並且我發現Android正在尋找一個在GridView上設置的監聽器。我想讓它在ListView上打電話給Listener,但我不知道如何強制它這樣做。

+0

把你的代碼....你在哪裏設置監聽器。 –

回答

1

GridView由於其佔用行的大部分空間,所以它的父母(ListView)變得不可觸及。 您需要使GridView不可滾動。它可以通過擴展GridView和ovveriding onMeasure方法做

public class NonScrollableGridView extends GridView { 
    public NonScrollableGridView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // Do not use the highest two bits of Integer.MAX_VALUE because they are 
     // reserved for the MeasureSpec mode 
     int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightSpec); 
     getLayoutParams().height = getMeasuredHeight(); 
    } 
} 

另一種選擇是使用RecyclerViewGridLayoutManager insted的的ListView,然後不使用GridView