android 多级分组,android 二级菜单(类似QQ分组)
注意點:1)適配器BaseExpandableListAdapter
2)數據源(數組、集合都行)(重點)
/**
*?Created?by?mb?on?2014/11/18.
*?二級菜單適配器
*/
public?class?MyExpandableListAdapter?extends?BaseExpandableListAdapter?{
private?Context?context;
List?groups;//String?[]?groups?=?{};
List>?childs;?//String?[]?[]?childs?=?{{},{},};
ViewHolder?holder;
public?MyExpandableListAdapter(Context?context,?List?groups,?List>?childs)?{
this.context?=?context;
this.groups?=?groups;
this.childs?=?childs;
}
@Override
public?int?getGroupCount()?{
return?groups.size();
}
@Override
public?int?getChildrenCount(int?groupPosition)?{
return?childs.get(groupPosition).size();
}
@Override
public?Object?getGroup(int?groupPosition)?{
return?groups.get(groupPosition);
}
@Override
public?Object?getChild(int?groupPosition,?int?childPosition)?{
return?childs.get(groupPosition).get(childPosition);
}
@Override
public?long?getGroupId(int?groupPosition)?{
return?groupPosition;
}
@Override
public?long?getChildId(int?groupPosition,?int?childPosition)?{
return?childPosition;
}
@Override
public?boolean?hasStableIds()?{
return?true;
}
@Override
public?View?getGroupView(int?groupPosition,?boolean?isExpanded,?View?convertView1,?ViewGroup?parent)?{
//自定義組視圖
return?convertView1;
}
@Override
public?View?getChildView(int?groupPosition,?int?childPosition,?boolean?isLastChild,?View?convertView,?ViewGroup?parent)?{
//自定義孩子視圖
return?convertView;
}
@Override
public?boolean?isChildSelectable(int?groupPosition,?int?childPosition)?{
return?true;
}
public?static?class?ViewHolder?{
//控件
}
}
事件監聽:
myExpandableListAdapter?=?new?MyExpandableListAdapter(context,?groups,?childs);
expandableListView.setAdapter(myExpandableListAdapter);
expandableListView.setOnGroupClickListener(new?ExpandableListView.OnGroupClickListener()?{
@Override
public?boolean?onGroupClick(ExpandableListView?parent,?View?v,?int?groupPosition,?long?id)?{
int?count?=?myExpandableListAdapter.getChildrenCount(groupPosition);
if?(count?<=?0)?{
activity.showCustomToast("...");
}
return?false;?//?---此處返回true??點擊后不能展開組??具體原因知道的留言
}
});
expandableListView.setOnChildClickListener(new?ExpandableListView.OnChildClickListener()?{
@Override
public?boolean?onChildClick(ExpandableListView?parent,?View?v,?int?groupPosition,?int?childPosition,?long?id)?{
return?false;
}
});
默認展開所有菜單:
for (int i = 0; i < myExpandableListAdapter.getGroupCount(); i++) {
expandableListView.expandGroup(i);
}
總結
以上是生活随笔為你收集整理的android 多级分组,android 二级菜单(类似QQ分组)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑开机启动修复无法自动修复此计算机,W
- 下一篇: 安装git安装路径在哪_Atom插件安装