上回書說到了ExpandableListView的基本用法,今天聊聊它的點擊事件
給組添加點擊事件
list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {
Toast.makeText(MainActivity.this, "點擊group", 0).show();
return false;//注意,這裏要返回false,否則你的列表將不能展開,因爲true是告訴系統不用再處理了。。
}
});
給子添加點擊事件
list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "點擊child", 0).show();
return true;
}
});
就這樣,簡單吧。
另外在網上看見了一些東西,比如list.setGroupIndicator(null);這樣會隱藏掉組列表的箭頭(雖然並沒看到效果),要是像做一個不需要擴展開的列表,可以先將組列表展開,然後在組點擊事件什麼都不做並返回true
for(int i=0;i<armTypes.length;i++){
list.expandGroup(i);
}
寫個循環遍歷所有組內容並展示,這樣程序運行後組列表就會是全部展開的樣式。