KHExpandableListAdapter.java
public class KHExpandableListAdapter extends BaseExpandableListAdapter {
private List<Map<String, Object>> groups;
private List<List<Map<String, Object>>> childs;
private Context context;
public KHExpandableListAdapter(Context context,List<Map<String, Object>> groups, List<List<Map<String, Object>>> childs){
this.context = context;
this.groups = groups;
this.childs = childs;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView childView = getGenericView();
childView.setText(childs.get(groupPosition).get(childPosition).get("child").toString());
return childView;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView groupView = getGenericView();
groupView.setText(groups.get(groupPosition).get("group").toString());
return groupView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 36);
TextView textView = new TextView(context);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
return textView;
}
}
ExpandableListViewActivity.java
public class ExpandableListViewActivity extends Activity {
private ExpandableListView ELV;
private List<Map<String,Object>> groups;
private List<List<Map<String,Object>>> childs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ELV = new ExpandableListView(this);
ELV.setBackgroundColor(Color.BLUE);
groups = new ArrayList<Map<String,Object>>();
childs = new ArrayList<List<Map<String,Object>>>();
List<Map<String,Object>> child1 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> child2 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> child3 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> child4 = new ArrayList<Map<String,Object>>();
for(int i = 0; i < 4; i++){
Map<String,Object> group = new HashMap<String,Object>();
group.put("group", "第 " + i + " 组");
groups.add(group);
}
for(int i = 0; i < 5; i++){
Map<String,Object> child = new HashMap<String,Object>();
child.put("child", "第 " + i + " 个child");
child1.add(child);
child2.add(child);
child3.add(child);
child4.add(child);
}
childs.add(child1);
childs.add(child2);
childs.add(child3);
childs.add(child4);
KHExpandableListAdapter KHA = new KHExpandableListAdapter(this,groups,childs);
ELV.setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Toast.makeText(ExpandableListViewActivity.this, childs.get(groupPosition).get(childPosition).get("child").toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("wf" , groups.get(groupPosition).get("group").toString());
intent.putExtra("user" , childs.get(groupPosition).get(childPosition).get("child").toString());
ExpandableListViewActivity.this.setResult(RESULT_OK, intent);
ExpandableListViewActivity.this.finish();
return false;
}
});
ELV.setAdapter(KHA);
this.setContentView(ELV);
}
}
<activity android:name=".ExpandableListViewActivity" android:theme="@android:style/Theme.Dialog"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.ELV" />
</intent-filter>
</activity>
分享到:
相关推荐
主要四部分内容:自定义ExpandableListView ,Json解析,短信,DatePickerDialog Json解析格式: { "help_count": "16", "date": "2012-06-15", "items": [ { "title":"PP(通讯达人)是什么?", "content":"PP...
Android实现自定义适配器的ExpandableListView示例,准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2",准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2",...
ExpandableListView展开折叠动画效果
ExpandableListView实现购物车页面
自定义ExpandableListView结合Sqlite,实现listview点击收缩效果
自定义expandablelistview一级标题,并实现类似radiobutton单选效果
本demo实现功能为:Expandablelistview子项checkBox先获取已选择状态,在次过程中可修改子项选择,最终提交选择子类的项,并打印相关数据
ExpandableListView 长按事件demo
android ExpandableListView三级菜单的使用,多级菜单下拉与隐藏,ExpandableListView的简单使用
一个ExpandableListView的例子,实现多级菜单分类展示.rar
CommentWithReplyView-master 基于ExpandableListView实现评论和回复的功能。 > 说明 ...处理了NestedScrollView、ExpandableListView和CoordinatorLayout的嵌套问题 点击某条评论,即可@ta进行回复
经常会遇到ExpandableListView这个控件,有的时候会有需求是当下拉列表子item被选中时要进行颜色的改变,以便标识
android ExpandableListView 四级树形目录 可无限延伸。
ExpandableListView中group和child自定义视图带CheckBox,实现child全选、部分选中功能,同时实现类似邮件群发的用户选择功能,选中的用户在EditText中显示,点击后移除. 更新: 1. ExpandableListView勾选后生成的...
ExpandableListView仿QQ好友列表
一个ExpandableListView的例子一个ExpandableListView的例子
Android ExpandableListView双层嵌套实现三级树形菜单