ExpandableListView
package com.foxconn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListViewDemo extends Activity {
private ExpandableListView exlv;
private List<Map<String, Contact>> groups;
private List<Map<String, List<Contact>>> childs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandablelistview);
exlv = (ExpandableListView) findViewById(R.id.exlv_list);
groups = new ArrayList<Map<String, Contact>>();
childs = new ArrayList<Map<String,List<Contact>>>();
initGroupsData();
for(int i = 0; i < groups.size();i++){
initChildsData();
}
ContactJoinAdapter mAdapter = new ContactJoinAdapter(this,groups,childs);
exlv.setAdapter(mAdapter);
exlv.setClickable(true);
exlv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Looper.prepare();
Toast.makeText(ExpandableListViewDemo.this, groups.get(groupPosition).get("group").getName(), Toast.LENGTH_SHORT).show();
// Looper.loop();
System.out.println("==>"+groupPosition);
return false;
}
});
exlv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(ExpandableListViewDemo.this, childs.get(groupPosition).get("child").get(childPosition).getName(), Toast.LENGTH_SHORT).show();
System.out.println("-->"+groupPosition+"-"+childPosition);
return false;
}
});
}
private void initChildsData() {
Map<String, List<Contact>> m1 = new HashMap<String, List<Contact>>();
List<Contact> list1 = new ArrayList<ExpandableListViewDemo.Contact>();
Contact c1 = new Contact("李1", R.drawable.e);
Contact c2 = new Contact("炸彈2", R.drawable.f);
Contact c3 = new Contact("的成3", R.drawable.g);
list1.add(c1);
list1.add(c2);
list1.add(c3);
m1.put("child", list1);
childs.add(m1);
}
private void initGroupsData() {
Contact c1 = new Contact("李", R.drawable.a);
Contact c2 = new Contact("炸彈", R.drawable.b);
Contact c3 = new Contact("的成", R.drawable.c);
Map<String, Contact> m1 = new HashMap<String, Contact>();
Map<String, Contact> m2 = new HashMap<String, Contact>();
Map<String, Contact> m3 = new HashMap<String, Contact>();
m1.put("group", c1);
m2.put("group", c2);
m3.put("group", c3);
groups.add(m1);
groups.add(m2);
groups.add(m3);
}
public class ContactJoinAdapter extends BaseExpandableListAdapter{
private Context context;
private List<Map<String, Contact>> mgroups;
private List<Map<String, List<Contact>>> mchilds;
private ChildView cv;
private GroupView gv;
private LayoutInflater inflater;
public class GroupView{
public ImageView gphoto;
public TextView gname;
public TextView gcount;
}
public class ChildView{
public ImageView cphoto;
public TextView cname;
public ImageButton check;
public ImageButton uncheck;
}
public ContactJoinAdapter(Context c,List<Map<String, Contact>> groups,List<Map<String, List<Contact>>> childs) {
inflater = LayoutInflater.from(c);
cv = new ChildView();
gv = new GroupView();
this.mgroups = groups;
this.mchilds = childs;
this.context = c;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mchilds.get(groupPosition).get("child").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) {
View cview = inflater.inflate(R.layout.exchild, null);
cv.cphoto = (ImageView) cview.findViewById(R.id.im_cphoto);
cv.cname = (TextView) cview.findViewById(R.id.tv_cname);
cv.check = (ImageButton) cview.findViewById(R.id.ibtn_check);
cv.uncheck = (ImageButton) cview.findViewById(R.id.ibtn_uncheck);
cv.cphoto.setImageResource(mchilds.get(groupPosition).get("child").get(childPosition).getPhotoId());
cv.cname.setText(childs.get(groupPosition).get("child").get(childPosition).getName());
cv.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv.check.setVisibility(View.GONE);
cv.uncheck.setVisibility(View.VISIBLE);
}
});
cv.uncheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv.uncheck.setVisibility(View.GONE);
cv.check.setVisibility(View.VISIBLE);
}
});
return cview;
}
@Override
public int getChildrenCount(int groupPosition) {
return mchilds.get(groupPosition).get("child").size();
}
@Override
public Object getGroup(int groupPosition) {
return mgroups.get(groupPosition).get("group");
}
@Override
public int getGroupCount() {
return mgroups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.exgroup, null);
gv.gphoto = (ImageView) view.findViewById(R.id.im_gphoto);
gv.gname = (TextView) view.findViewById(R.id.tv_gname);
gv.gcount = (TextView) view.findViewById(R.id.tv_childcount);
gv.gphoto.setImageResource(mgroups.get(groupPosition).get("group").getPhotoId());
gv.gname.setText(mgroups.get(groupPosition).get("group").getName());
gv.gcount.setText("3");
//super.onCreate(savedInstanceState);
return view;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
public class Contact{
private String name;
private int photoId;
public Contact(String n,int i) {
this.name = n;
this.photoId = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhotoId() {
return photoId;
}
public void setPhotoId(int photoId) {
this.photoId = photoId;
}
}
// @Override
// public boolean onChildClick(ExpandableListView parent, View v,
// int groupPosition, int childPosition, long id) {
// Toast.makeText(this, childs.get(groupPosition).get("child").get(childPosition).getName(), Toast.LENGTH_SHORT).show();
// return true;
// }
}
expandablelistview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView
android:id="@+id/exlv_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@+id/join_empty"
android:text="No Data"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>
exgroup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/im_gphoto"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/a"
android:layout_marginLeft="33dip"
/>
<TextView
android:id="@+id/tv_gname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/im_photo"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/tv_childcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_name"
android:layout_marginLeft="12dip"
android:layout_alignParentRight="true"
android:textColor="@android:color/darker_gray"
/>
</RelativeLayout>
exchild.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/im_cphoto"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/c"
android:layout_marginLeft="50dip"
/>
<TextView
android:id="@+id/tv_cname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/im_photo"
android:layout_marginLeft="8dip"
android:layout_centerHorizontal="true"
/>
<ImageButton
android:id="@+id/ibtn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_name"
android:visibility="gone"
android:src="@drawable/check"
android:layout_marginRight="12dip"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="@+id/ibtn_uncheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_name"
android:layout_marginRight="12dip"
android:src="@drawable/uncheck"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListViewDemo extends Activity {
private ExpandableListView exlv;
private List<Map<String, Contact>> groups;
private List<Map<String, List<Contact>>> childs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandablelistview);
exlv = (ExpandableListView) findViewById(R.id.exlv_list);
groups = new ArrayList<Map<String, Contact>>();
childs = new ArrayList<Map<String,List<Contact>>>();
initGroupsData();
for(int i = 0; i < groups.size();i++){
initChildsData();
}
ContactJoinAdapter mAdapter = new ContactJoinAdapter(this,groups,childs);
exlv.setAdapter(mAdapter);
exlv.setClickable(true);
exlv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Looper.prepare();
Toast.makeText(ExpandableListViewDemo.this, groups.get(groupPosition).get("group").getName(), Toast.LENGTH_SHORT).show();
// Looper.loop();
System.out.println("==>"+groupPosition);
return false;
}
});
exlv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(ExpandableListViewDemo.this, childs.get(groupPosition).get("child").get(childPosition).getName(), Toast.LENGTH_SHORT).show();
System.out.println("-->"+groupPosition+"-"+childPosition);
return false;
}
});
}
private void initChildsData() {
Map<String, List<Contact>> m1 = new HashMap<String, List<Contact>>();
List<Contact> list1 = new ArrayList<ExpandableListViewDemo.Contact>();
Contact c1 = new Contact("李1", R.drawable.e);
Contact c2 = new Contact("炸彈2", R.drawable.f);
Contact c3 = new Contact("的成3", R.drawable.g);
list1.add(c1);
list1.add(c2);
list1.add(c3);
m1.put("child", list1);
childs.add(m1);
}
private void initGroupsData() {
Contact c1 = new Contact("李", R.drawable.a);
Contact c2 = new Contact("炸彈", R.drawable.b);
Contact c3 = new Contact("的成", R.drawable.c);
Map<String, Contact> m1 = new HashMap<String, Contact>();
Map<String, Contact> m2 = new HashMap<String, Contact>();
Map<String, Contact> m3 = new HashMap<String, Contact>();
m1.put("group", c1);
m2.put("group", c2);
m3.put("group", c3);
groups.add(m1);
groups.add(m2);
groups.add(m3);
}
public class ContactJoinAdapter extends BaseExpandableListAdapter{
private Context context;
private List<Map<String, Contact>> mgroups;
private List<Map<String, List<Contact>>> mchilds;
private ChildView cv;
private GroupView gv;
private LayoutInflater inflater;
public class GroupView{
public ImageView gphoto;
public TextView gname;
public TextView gcount;
}
public class ChildView{
public ImageView cphoto;
public TextView cname;
public ImageButton check;
public ImageButton uncheck;
}
public ContactJoinAdapter(Context c,List<Map<String, Contact>> groups,List<Map<String, List<Contact>>> childs) {
inflater = LayoutInflater.from(c);
cv = new ChildView();
gv = new GroupView();
this.mgroups = groups;
this.mchilds = childs;
this.context = c;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mchilds.get(groupPosition).get("child").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) {
View cview = inflater.inflate(R.layout.exchild, null);
cv.cphoto = (ImageView) cview.findViewById(R.id.im_cphoto);
cv.cname = (TextView) cview.findViewById(R.id.tv_cname);
cv.check = (ImageButton) cview.findViewById(R.id.ibtn_check);
cv.uncheck = (ImageButton) cview.findViewById(R.id.ibtn_uncheck);
cv.cphoto.setImageResource(mchilds.get(groupPosition).get("child").get(childPosition).getPhotoId());
cv.cname.setText(childs.get(groupPosition).get("child").get(childPosition).getName());
cv.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv.check.setVisibility(View.GONE);
cv.uncheck.setVisibility(View.VISIBLE);
}
});
cv.uncheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv.uncheck.setVisibility(View.GONE);
cv.check.setVisibility(View.VISIBLE);
}
});
return cview;
}
@Override
public int getChildrenCount(int groupPosition) {
return mchilds.get(groupPosition).get("child").size();
}
@Override
public Object getGroup(int groupPosition) {
return mgroups.get(groupPosition).get("group");
}
@Override
public int getGroupCount() {
return mgroups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.exgroup, null);
gv.gphoto = (ImageView) view.findViewById(R.id.im_gphoto);
gv.gname = (TextView) view.findViewById(R.id.tv_gname);
gv.gcount = (TextView) view.findViewById(R.id.tv_childcount);
gv.gphoto.setImageResource(mgroups.get(groupPosition).get("group").getPhotoId());
gv.gname.setText(mgroups.get(groupPosition).get("group").getName());
gv.gcount.setText("3");
//super.onCreate(savedInstanceState);
return view;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
public class Contact{
private String name;
private int photoId;
public Contact(String n,int i) {
this.name = n;
this.photoId = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhotoId() {
return photoId;
}
public void setPhotoId(int photoId) {
this.photoId = photoId;
}
}
// @Override
// public boolean onChildClick(ExpandableListView parent, View v,
// int groupPosition, int childPosition, long id) {
// Toast.makeText(this, childs.get(groupPosition).get("child").get(childPosition).getName(), Toast.LENGTH_SHORT).show();
// return true;
// }
}
expandablelistview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView
android:id="@+id/exlv_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@+id/join_empty"
android:text="No Data"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>
exgroup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/im_gphoto"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/a"
android:layout_marginLeft="33dip"
/>
<TextView
android:id="@+id/tv_gname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/im_photo"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/tv_childcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_name"
android:layout_marginLeft="12dip"
android:layout_alignParentRight="true"
android:textColor="@android:color/darker_gray"
/>
</RelativeLayout>
exchild.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/im_cphoto"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/c"
android:layout_marginLeft="50dip"
/>
<TextView
android:id="@+id/tv_cname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/im_photo"
android:layout_marginLeft="8dip"
android:layout_centerHorizontal="true"
/>
<ImageButton
android:id="@+id/ibtn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_name"
android:visibility="gone"
android:src="@drawable/check"
android:layout_marginRight="12dip"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="@+id/ibtn_uncheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_name"
android:layout_marginRight="12dip"
android:src="@drawable/uncheck"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
相关推荐
在Android开发中,`ExpandableListView`是一种可扩展的列表视图,允许用户折叠和展开子项,这对于显示层次结构的数据非常有用。本教程将深入探讨如何自定义`ExpandableListView`并结合SQLite数据库来实现数据的动态...
在Android开发中,ExpandableListView是一个非常有用的控件,它允许我们展示可折叠的列表,通常用于层次结构的数据展示。本教程将详细讲解如何自定义ExpandableListView,使其一级标题具有类似于RadioButton的单选...
在Android应用开发中,"ExpandableListView实现购物车页面"是一个常见的需求,它涉及到用户界面设计、数据管理和交互。ExpandableListView是Android SDK提供的一种可扩展的列表视图,允许用户展示分组数据,每组内...
在Android开发中,`ExpandableListView`是一种常用的控件,用于展示可以展开和折叠的列表。这个控件允许用户在一级列表项下显示更详细的数据,通常用于构建层级结构清晰的列表,例如菜单、目录或者分类信息。在给定...
"ExpandableListView实现二级列表购物车" ExpandableListView是Android中的一种视图组件,用于显示二级列表的数据。它可以将数据分组显示,方便用户浏览和操作。下面我们来详细介绍如何使用ExpandableListView实现...
在Android开发中,`ExpandableListView`是一种常用的控件,它允许用户展示可折叠的列表,其中每个父项可以展开显示多个子项。这个控件非常适合于需要层次结构数据展示的应用,例如目录树、菜单或者分类信息。在这个...
在Android开发中,`ExpandableListView`是一种常用的控件,它可以展示可折叠的子列表,类似于QQ应用中的联系人列表。这个"ExpandableListView仿QQ列表的实现" demo旨在教你如何利用`ExpandableListView`创建类似QQ...
在Android开发中,ExpandableListView是一个非常常用的控件,它允许我们展示层次结构的数据,比如一个树状结构。然而,在实际使用过程中,开发者经常会遇到一些问题,如子级条目的背景色混乱以及如何实现父级展开项...
在Android开发中,`ExpandableListView`是一种常用的控件,它允许用户展开和折叠分组,每个分组下可以包含多个子项。这种控件在显示层次结构数据时非常实用,比如目录结构、菜单列表或者分类信息展示。下面将详细...
在Android开发中,`ExpandableListView`是一种常用的控件,它可以展示可展开和折叠的列表,通常用于构建具有层级结构的数据展示。在这个“android ExpandableListView三级菜单的使用”示例中,我们将深入探讨如何...
在Android开发中,`ExpandableListView`是一种常用的控件,它允许我们展示具有层次结构的数据,比如一个多级菜单分类的场景。在这个例子中,我们将会深入探讨如何利用`ExpandableListView`来创建一个可展开和折叠的...
主要四部分内容:自定义ExpandableListView ,Json解析,短信,DatePickerDialog Json解析格式: { "help_count": "16", "date": "2012-06-15", "items": [ { "title":"PP(通讯达人)是什么?", "content":"PP...
在Android开发中,`ExpandableListView`是一种常用的控件,它允许用户展开和折叠分组,每个分组下可以有多个子项。当涉及到用户体验优化时,改变`ExpandableListView`子item选中状态的颜色是一种常见做法,可以提高...
在Android开发中,`ExpandableListView`是一种常用的控件,用于展示可以展开和折叠的列表,通常用于构建层级结构的数据展示。在这个特定的实现中,我们关注的是如何去除下拉标志,并实现点击新组时自动关闭其他已...
在Android开发中,`ExpandableListView`是一种常用的控件,它允许用户展开和折叠不同的组,每个组下还可以包含多个子项。这个控件在显示层次结构数据时非常有用,例如菜单、设置列表等。而将`CheckBox`与`...
在Android开发中,`ExpandableListView`、`RecyclerView`以及`ListView`是三种常见的列表控件,用于展示可滚动的数据集合。本篇文章将深入探讨这三个组件的使用方法、特点及它们之间的区别。 首先,`...
在Android开发中,`ExpandableListView`是一种非常实用的控件,它允许开发者创建具有可扩展行的列表,也就是我们常说的嵌套列表。这种控件可以用来展示层次结构的数据,比如目录结构、菜单选项或者复杂的分类信息。...
Android实现自定义适配器的ExpandableListView示例,准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2",准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2",...