ExpandableListActivy是用于显示二级列表的详细信息,与ListActivity类似。今天学习了一下这个类,先上效果图
其他的Group打开与上面的图类似,在使用ExpandableListActivity类时,往往需要三个布局文件来实现,第一个局部文件,无可厚非是ExpandableListView控件所在的布局文件,这里我的是socket_devices.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" android:background="#eee" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="45dp" android:id="@+id/title" android:background="@drawable/title_bar" android:gravity="center_vertical" > <Button android:id="@+id/back" android:text="返回" android:textColor="#ffffff" android:textSize="15sp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_width="80dp" android:layout_height="wrap_content" android:background="@drawable/title_btn_back" android:onClick="goBack" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="设备管理" android:textSize="20sp" android:textColor="#ffffff" /> <TextView android:id="@+id/userName" android:text="用户名" android:textColor="#ffffff" android:textSize="15sp" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/title_btn_right" /> </RelativeLayout> <ExpandableListView android:layout_below="@id/title" android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#eee" /> </RelativeLayout>
第二个布局文件是Group层显示的控件布局一般而言,用一个TextView来就可以了,我的如下device_items.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/device_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingLeft="50dp" android:paddingBottom="8dp" android:textColor="#000000" android:textSize="20dp" /> </LinearLayout>在效果图中它就是用来显示home(n),最后一个布局文件就是子层信息显示的布局了。这个可以随便定义,把自己所需要显示的信息控件放在这里就行了,这里我只用了三个控件,如下socket_items.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:background="@drawable/preference_item" > <ImageView android:id="@+id/email_remind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="matrix" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:src="@drawable/default_bottle" /> <TextView android:id="@+id/socket_tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/email_remind" android:layout_centerVertical="true" android:padding="8dp" android:text="" android:textSize="17sp" android:textColor="#000" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="matrix" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:clickable="true" android:src="@drawable/mm_submenu" /> </RelativeLayout> </LinearLayout>
public class SocketDeviceList extends ExpandableListActivity { private MySimpleExpandableListAdapter adapter = null;//适配器 List<HashMap<String, String>> device_groups = null;//父层数据 List<List<HashMap<String, String>>> items_child = null;//子层数据 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.socket_devices); device_groups = new ArrayList<HashMap<String, String>>(); items_child = new ArrayList<List<HashMap<String, String>>>(); for(int i = 0; i < 4; i++) { HashMap<String, String> items = new HashMap<String, String>(); items.put("device_group", "home" + i); device_groups.add(items); List<HashMap<String, String>> lists = new ArrayList<HashMap<String, String>>(); for(int j = 0; j < 5; j++) { HashMap<String, String> item = new HashMap<String, String>(); item.put("item_group", "socket" + j); lists.add(item); } items_child.add(lists); } /** * SimpleExpandableListAdapter的参数那是相当的多啊 * 参数 1:context * 2:父级目录的数据 * 3:父级目录的布局文件 * 4: 夫级目录的数据来源 * 5:指定父级目录显示数据的控件 * 6:子级目录的数据 * 7:子级目录的布局文件 * 8:子级目录的数据来源 * 9:指定子级目录显示数据的控件 */ adapter = new MySimpleExpandableListAdapter( this, device_groups, R.layout.device_items, new String[]{"device_group"}, new int[] {R.id.device_group}, items_child, R.layout.socket_items, new String[]{"item_group"}, new int[] {R.id.socket_tv1}); setListAdapter(adapter); getExpandableListView().setGroupIndicator(getResources() .getDrawable(R.drawable.expander_ic_folder)); getExpandableListView().setOnGroupClickListener(new OnGroupClickListener() { public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // TODO Auto-generated method stub Toast.makeText(SocketDeviceList.this, "test", Toast.LENGTH_SHORT).show(); return false; } }); getExpandableListView().setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub Toast.makeText(SocketDeviceList.this, items_child.get(groupPosition).get(childPosition).get("item_group"), Toast.LENGTH_SHORT).show(); return false; } }); } public class MySimpleExpandableListAdapter extends SimpleExpandableListAdapter { public MySimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, int lastChildLayout, String[] childFrom, int[] childTo) { super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, childLayout, lastChildLayout, childFrom, childTo); // TODO Auto-generated constructor stub } public MySimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo) { super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo); // TODO Auto-generated constructor stub }//use this public MySimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo) { super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo); // TODO Auto-generated constructor stub } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub //return super.getChild(groupPosition, childPosition); return items_child.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent); } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return items_child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return device_groups.get(groupPosition); } @Override public int getGroupCount() { // TODO Auto-generated method stub return device_groups.size(); } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return super.getGroupView(groupPosition, isExpanded, convertView, parent); } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } } }
adapter = new MySimpleExpandableListAdapter( this, device_groups, R.layout.device_items, new String[]{"device_group"}, new int[] {R.id.device_group}, items_child, R.layout.socket_items, new String[]{"item_group"}, new int[] {R.id.socket_tv1}); setListAdapter(adapter);
getExpandableListView().setGroupIndicator(getResources() .getDrawable(R.drawable.expander_ic_folder));
PS:我在用的过程中出现过单击child没有响应的情况,首先得确定你的child的布局中没有指定clickable属性,只要不影响到你的TextView控件就行,一开始我是设置Relativelayout为可点击,因此才导致child单击没有响应的,