天天看點

關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題

嗯,第一次發文章,請多多指教。

好的,正文開始:

ExpandableListView控件的單擊事件的要求大多數大神都已經說過了

1. BaseExpandableListAdapter的重寫方法isChildSelectable必須為true。

2.ExpandableListView控件實作ExpandableListView.OnChildClickListener接口,綁定單擊事件。

3.當ExpandableListView的子項是Button,CheckBox等本身聚焦的控件,将控件的setFocusable方法設為false,禁用控件的聚焦

上面的内容是和大神們一樣的。

說點我自己實驗的東西吧

1.ExpandableListView的子項是本身具有聚焦的控件時,比如Button控件,如果ExpandableListView子項單擊事件沒有成功,禁用setFocusable後還是沒有成功,那就是Button本身的單擊事件成功運作了,而不是控件本身的聚焦問題,是以這時候應該将setClickable設為false,就可以了

實驗代碼圖檔

關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題
關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題

結果

關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題

從上面的三張圖可以看出ExpandableListView單擊事件并沒有運作,而是Button的單擊事件運作了,是以這跟Button的焦點本身沒有問題

下面設Button的setClickable為false,圖檔如下

關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題
關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題
關于Android中ExpandableListView子項是Button控件而單擊事件沒有響應的問題

從上面的實驗結果來看是因為點選時點選的是Button而不是ExpandableListView的子項,是以響應的自然是Button的OnClickListener,而不是ExpandableListView的OnChildClickListener,這時候設Button的setClickable為false,點選的自然ExpandableListView本身的子項,而不是Button。

哭,感覺還是沒有說好,請不要噴的太厲害,有問題的地方請多多指教(感覺還是沒有把重點把握好)。