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

本讲内容:ExpandableListView,ExpandableListActivity 可扩展列表 二级列表 手风琴效果accordion
ExpandableListView的效果很实用,配置时有那么一点啰嗦,也容易出错,我在这里例子里会尽量去掉所有干扰信息,好让大家使用时容易借鉴。好我们先看一下运行效果:

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

点击二层列表(嵌套的列表)的某一项:
下面我们来看代码:
1、新建一个项目 Lesson43_ExpandableListView
2、main.xml 的内容如下:
<?xml version="1.0" encoding="utf-8"?> <LINEARLAYOUT xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EXPANDABLELISTVIEW android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@id/android:list"> </EXPANDABLELISTVIEW> </LINEARLAYOUT>
请注意ExpandableListView标签中id的写法是固定的@id/android:list,因为我们这里用的是ExpandableListActivity,而ExpandableListActivity代码里写死了要寻找的UI元素是它,这和我们以前讲的很多特殊的Activity是一样的,这里再稍作提醒。
3、MainActivity.java的代码如下,解释在注释里:
package basic.android.lesson43;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;
public class MainActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 准备顶层列表数据
List
<MAP string><STRING ,>> topList = new ArrayList</STRING></MAP>
<MAP string><STRING ,>>();
Map</STRING><STRING string ,> topMap1 = new HashMap</STRING><STRING string ,>();
Map</STRING><STRING string ,> topMap2 = new HashMap</STRING><STRING string ,>();
topMap1.put("month", "三月测评项");
topMap2.put("month", "四月测评项");
topList.add(topMap1);
topList.add(topMap2);
// 准备二层列表数据
List
<LIST string>
<MAP><STRING ,>>> nestList = new ArrayList</STRING></MAP>
</LIST>
<LIST string>
<MAP><STRING ,>>>();
// 准备二层列表第一个子列表数据
List
<MAP string><STRING ,>> nestList1 = new ArrayList</STRING></MAP>
<MAP string><STRING ,>>();
Map</STRING><STRING string ,> nestMap1 = new HashMap</STRING><STRING string ,>();
Map</STRING><STRING string ,> nestMap2 = new HashMap</STRING><STRING string ,>();
Map</STRING><STRING string ,> nestMap3 = new HashMap</STRING><STRING string ,>();
nestMap1.put("test", "看手");
nestMap2.put("test", "吃手");
nestMap3.put("test", "玩手");
nestList1.add(nestMap1);
nestList1.add(nestMap2);
nestList1.add(nestMap3);
// 准备二层列表第二个子列表数据
List
<MAP string><STRING ,>> nestList2 = new ArrayList</STRING></MAP>
<MAP string><STRING ,>>();
Map</STRING><STRING string ,> nestMap4 = new HashMap</STRING><STRING string ,>();
Map</STRING><STRING string ,> nestMap5 = new HashMap</STRING><STRING string ,>();
nestMap4.put("test", "翻身");
nestMap5.put("test", "辨别声音来源方位");
nestList2.add(nestMap4);
nestList2.add(nestMap5);
// 把子列表数据放入
nestList.add(nestList1);
nestList.add(nestList2);
// 准备数据匹配器
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this, //1.上下文
topList, //2.顶层数据列表
android.R.layout.simple_expandable_list_item_1, // 3.一层显示样式
new String[]{"month"}, //4.顶层map的键
new int[]{android.R.id.text1}, // 5.顶层数据显示的View ID
nestList, //6.二层数据列表
android.R.layout.simple_list_item_1, //7.二层显示样式
new String[]{"test"}, //8.二层map的键
new int[]{android.R.id.text1} //9.二层数据显示的View ID
);
//设置数据匹配器
this.setListAdapter(adapter);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(this, "嵌套列表被点击,顶层列表定位"+groupPosition+"二层列表定位"+childPosition, Toast.LENGTH_LONG).show();
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(this, "顶层列表收缩,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
super.onGroupCollapse(groupPosition);
}
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(this, "顶层列表展开,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
super.onGroupExpand(groupPosition);
}
}
</STRING></MAP>
</STRING></MAP>
</STRING></MAP>
</LIST></STRING></MAP>4、编译并运行程序即可看到上面的效果。
那么本节课就到这里了,Android中很多内容就像本节的ExpandableListView一样讨厌,来,我们一起鄙视一下^_^
附件列表
您所在的用户组无法下载或查看附件
Java-Android手机千人开发交流QQ群:38088312,PHP开发千人高级交流QQ群:50194090,欢迎加入学习!本站为
非赢利站点,挖掘网络资源,分享个人兴趣,如有侵犯您的版权,请联系我们,我们会第一时间删除内容或添加转载出处,敬请谅解!
如果您认为本词条还有待完善,请 编辑
