moss

第四十三讲:用户界面 View(十)

本讲内容:ExpandableListView,ExpandableListActivity 可扩展列表 二级列表 手风琴效果accordion

本讲源代码下载:Lesson43_ExpandableListView

ExpandableListView的效果很实用,配置时有那么一点啰嗦,也容易出错,我在这里例子里会尽量去掉所有干扰信息,好让大家使用时容易借鉴。好我们先看一下运行效果:

image

点击一级列表,展开下一级:

image

点击二层列表(嵌套的列表)的某一项:

image

下面我们来看代码:

1、新建一个项目 Lesson43_ExpandableListView

2、main.xml 的内容如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3     <expandablelistview android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content">
4     </expandablelistview>
5 </linearlayout>

请注意ExpandableListView标签中id的写法是固定的@id/android:list,因为我们这里用的是ExpandableListActivity,而ExpandableListActivity代码里写死了要寻找的UI元素是它,这和我们以前讲的很多特殊的Activity是一样的,这里再稍作提醒。

3、MainActivity.java的代码如下,解释在注释里:

001 package basic.android.lesson43;
002
003 import java.util.ArrayList;
004 import java.util.HashMap;
005 import java.util.List;
006 import java.util.Map;
007
008 import android.app.ExpandableListActivity;
009 import android.os.Bundle;
010 import android.view.View;
011 import android.widget.ExpandableListView;
012 import android.widget.SimpleExpandableListAdapter;
013 import android.widget.Toast;
014
015 public class MainActivity extends ExpandableListActivity {
016
017     @Override
018     public void onCreate(Bundle savedInstanceState) {
019         super.onCreate(savedInstanceState);
020         setContentView(R.layout.main);
021
022         // 准备顶层列表数据
023         List
024 <map string=""><string ,="">> topList = new ArrayList</string></map>
025 <map string=""><string ,="">>();
026
027         Map</string><string string="" ,=""> topMap1 = new HashMap</string><string string="" ,="">();
028         Map</string><string string="" ,=""> topMap2 = new HashMap</string><string string="" ,="">();
029         topMap1.put("month", "三月测评项");
030         topMap2.put("month", "四月测评项");
031         topList.add(topMap1);
032         topList.add(topMap2);
033
034         // 准备二层列表数据
035         List
036 <list string="">
037 <map><string ,="">>> nestList = new ArrayList</string></map>
038 </list>
039 <list string="">
040 <map><string ,="">>>();
041
042         // 准备二层列表第一个子列表数据
043         List
044 <map string=""><string ,="">> nestList1 = new ArrayList</string></map>
045 <map string=""><string ,="">>();
046         Map</string><string string="" ,=""> nestMap1 = new HashMap</string><string string="" ,="">();
047         Map</string><string string="" ,=""> nestMap2 = new HashMap</string><string string="" ,="">();
048         Map</string><string string="" ,=""> nestMap3 = new HashMap</string><string string="" ,="">();
049         nestMap1.put("test", "看手");
050         nestMap2.put("test", "吃手");
051         nestMap3.put("test", "玩手");
052         nestList1.add(nestMap1);
053         nestList1.add(nestMap2);
054         nestList1.add(nestMap3);
055
056         // 准备二层列表第二个子列表数据
057         List
058 <map string=""><string ,="">> nestList2 = new ArrayList</string></map>
059 <map string=""><string ,="">>();
060         Map</string><string string="" ,=""> nestMap4 = new HashMap</string><string string="" ,="">();
061         Map</string><string string="" ,=""> nestMap5 = new HashMap</string><string string="" ,="">();
062         nestMap4.put("test", "翻身");
063         nestMap5.put("test", "辨别声音来源方位");
064         nestList2.add(nestMap4);
065         nestList2.add(nestMap5);
066
067         // 把子列表数据放入
068         nestList.add(nestList1);
069         nestList.add(nestList2);
070
071         // 准备数据匹配器
072         SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
073                 this, //1.上下文
074                 topList, //2.顶层数据列表
075                 android.R.layout.simple_expandable_list_item_1, // 3.一层显示样式
076                 new String[]{"month"}, //4.顶层map的键
077                 new int[]{android.R.id.text1}, // 5.顶层数据显示的View ID
078                 nestList, //6.二层数据列表
079                 android.R.layout.simple_list_item_1, //7.二层显示样式
080                 new String[]{"test"}, //8.二层map的键
081                 new int[]{android.R.id.text1} //9.二层数据显示的View ID
082                 );
083
084         //设置数据匹配器
085         this.setListAdapter(adapter);
086
087     }
088
089     @Override
090     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
091         Toast.makeText(this, "嵌套列表被点击,顶层列表定位"+groupPosition+"二层列表定位"+childPosition, Toast.LENGTH_LONG).show();
092         return super.onChildClick(parent, v, groupPosition, childPosition, id);
093     }
094
095     @Override
096     public void onGroupCollapse(int groupPosition) {
097         Toast.makeText(this, "顶层列表收缩,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
098         super.onGroupCollapse(groupPosition);
099     }
100
101     @Override
102     public void onGroupExpand(int groupPosition) {
103         Toast.makeText(this, "顶层列表展开,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
104         super.onGroupExpand(groupPosition);
105     }
106
107 }
108 </string></map>
109
110 </string></map>
111
112 </string></map>
113 </list></string></map>

4、编译并运行程序即可看到上面的效果。

那么本节课就到这里了,Android中很多内容就像本节的ExpandableListView一样讨厌,来,我们一起鄙视一下^_^

转自 http://android.yaohuiji.com/archives/2511

分类:

技术点:

相关文章: