如何在一个有限的屏幕上加载多页的信息,除此之外还可以通过隐藏-展开的方式,在屏幕有限的空间内包含更多的现象,如图所示,这就是ExpandableListView。
ExpandableListView,具有树的结构:Groups和childrens。下面我们通过一个简单的例子来学习,这个例子的数据不再采用String[],而是采用另一个常见的HashMap方式,顺带复习一下。
public class Chapter9Tutorial4extends ExpandableListActivity{
private static final String NAME="NAME";//这是HashMap的Key名称
private static final String IS_EVEN = "IS_EVEN";//这是HashMap的Key名称
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//步骤1:设置ExpandableListActivity的Group和Chlid的数据
//在这个例子中,Group的单元格式为HashMap<String,String>所以其格式为List<单元格式
//Child中最基础的item格式也为HashMap<String,String>,因此每一个Group中包含的child的格式为List<基础单元格式>,而所有的Group又是List,因此所有的child的格式为List<每个Group的child格式>,即List<List<基础单元格式>>
List<Map<String ,String>> groupData = new ArrayList<Map<String,String>>();
List<List<Map<String ,String>>> childData = new ArrayList<List<Map<String ,String>>>();
for(int i = 0 ; i< 6 ; i++){
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " +i);
curGroupMap.put(IS_EVEN, "Hello " + i);
List<Map<String,String>> children = new ArrayList<Map<String,String>>();
for(int j = 0; j < 3; j++){
Map<String ,String > curChildMap = new HashMap<String,String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j + " for group " + i);
curChildMap.put(IS_EVEN, "From Group " + i + " Hello my friend!");
}
childData.add(children);
}
//步骤2:设置ExpandableList的adapter,ExpandableListAdapter是个接口,对于Map方式可使用SimpleExpandableListAdapter
ExpandableListAdaptermAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[]{NAME,IS_EVEN},
new int[] {android.R.id.text1,android.R.id.text2},
childData,
android.R.layout.simple_expandable_list_item_2,
new String[]{NAME,IS_EVEN},
new int[] {android.R.id.text1,android.R.id.text2}
);
setListAdapter(mAdapter);
}
//步骤3:触发处理,包括有:OnChildClickListener,OnGroupClickListener,OnGroupCollapseListener和OnGroupExpandListener。在这个例子中每个child的View的类型是TwoLineListItem
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.d("WEI","Click: [" + groupPosition + "," + childPosition + "] ");
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
}
分享到:
相关推荐
Android学习笔记(二五): 多信息显示-ExpandableListView的使用
Android项目实战--手机卫士33--ExpandableListView的使用
android--ExpandableListView实例
Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友
本人博客:Android常用控件(能折叠的ListView)--ExpandableListView的使用模仿QQ好友列表的demo
换种思路解决列表套列表-ExpandableListView的使用,支持上拉加载,更改数据源
Android 时光轴 expandablelistview
时间轴demo,可以后期根据需求 自己改动界面和bean数据结构。
ExpandableListView实现购物车页面.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
CommentWithReplyView-master 基于ExpandableListView实现评论和... > 说明 ...material design风格的详情页 处理了NestedScrollView、ExpandableListView和CoordinatorLayout的嵌套问题 点击某条评论,即可@ta进行回复
Android之ExpandableListView控件的使用
可以扩展的listview,类似于手机qq分组的listview(自己在网上找demo,找到的全是一些文档,好不容易把它拼成了一个demo,决定跟大家分享)
Android之ExpandableListView笔记
比LIStVIEw更好,适合初学者下载;很实用;LIStVIEw的升级
android ExpandableListView三级菜单的使用,多级菜单下拉与隐藏,ExpandableListView的简单使用
使用ExpandableListView实现的购物车,解决了EditText输入与焦点错乱问题,用二阶贝塞尔曲线实现购物车动画
android ExpandableListView简单例子
android中ExpandableListView嵌套GridView的使用
Android实现自定义适配器的ExpandableListView示例,准备一级列表中显示的数据:2个一级列表,分别显示"group1"和"group2",准备第一个一级列表中的二级列表数据:两个二级列表,分别显示"childData1"和"childData2",...
本demo实现功能为:Expandablelistview子项checkBox先获取已选择状态,在次过程中可修改子项选择,最终提交选择子类的项,并打印相关数据