ExpandableListView如何进行二级扩展(SimpleExpandableListAdapter)
2016-03-29 17:23
381 查看
在网上搜索,发现现在在操作ListView上面,进行二级扩展的是ExpandableListView,但一般会有三种方式进行操作。
一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView
二是 扩展BaseExpandableListAdpter
三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter。
这里面先说一下:SimpleExpandableListAdpater操作
首先我们需要知道一级菜单和二级菜单,一般我们以groups为一级,childs为子级。
先看下,groups.xml布局:
子级布局文件childs.xml
显示当前界面的布局文件,我是以steam.xml文件命名的
我本人是在Fragment里面进行操作二级扩展的和普通的activity有点不同,但大同小异。java文件
这里面可能需要自己补充知识,关于如何在activity里面操作Fragement.
这样就可以做出来一个简单的二级展开。
但是这样我发现子级如果需要添加图片,文字和其他自定义的扩展,这个方法不是很合适,只能做元素只有一个子级。(不知道我说的对不对,希望有新用法的朋友给我补充下)
一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView
二是 扩展BaseExpandableListAdpter
三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter。
这里面先说一下:SimpleExpandableListAdpater操作
首先我们需要知道一级菜单和二级菜单,一般我们以groups为一级,childs为子级。
先看下,groups.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" > <TextView android:id="@+id/textGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="40px" android:paddingTop="6px" android:paddingBottom="6px" android:textSize="25sp" android:text="No data" /> </LinearLayout>
子级布局文件childs.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textChild" android:layout_width="200dp" android:layout_height="wrap_content" android:paddingLeft="60px" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="20sp" android:text="No Data" android:layout_marginLeft="0dp"/> </LinearLayout>
显示当前界面的布局文件,我是以steam.xml文件命名的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/expandable" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
我本人是在Fragment里面进行操作二级扩展的和普通的activity有点不同,但大同小异。java文件
public class StreamFragment extends Fragment { List<Map<String, String>> gruops = new ArrayList<Map<String, String>>(); List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); private ExpandableListView ep; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View messageLayout = inflater.inflate(R.layout.stream, container, false); ep = (ExpandableListView) messageLayout.findViewById(R.id.expandable); setListData(); return messageLayout; } /** * 设置列表内容 */ public void setListData() { // 创建一级条目标题 String[] names ={"1月","2月"}; //创建二级标题 String[][] child_names= {{"费用","衣服","用品"},{"老家", "学校","美丽"}}; for(int i=0;i<names.length;i++){ Map<String,String> namedata = new HashMap<String ,String>(); namedata.put("names",names[i]); gruops.add(namedata); List<Map<String, String>> child_map = new ArrayList<Map<String, String>>(); for(int j=0;j<child_names[0].length;j++) { Map<String,String> mapcs = new HashMap<String,String>(); mapcs.put("child_names",child_names[i][j]); child_map.add(mapcs); // System.out.println(child_map); } childs.add(child_map); // System.out.println(childs); } /** * 创建ExpandableList的Adapter容器 参数: 1.上下文 2.一级集合 3.一级样式文件 4. 一级条目键值 * 5.一级显示控件名 6. 二级集合 7. 二级样式 8.二级条目键值 9.二级显示控件名 * */ SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter( getActivity(), gruops, R.layout.groups, new String[]{"names"}, new int[]{R.id.textGroup}, childs, R.layout.childs, new String[]{"child_names"}, new int[]{R.id.textChild,R.id.imageChild}); // 加入列表 ep.setAdapter(sela); } }
这里面可能需要自己补充知识,关于如何在activity里面操作Fragement.
这样就可以做出来一个简单的二级展开。
但是这样我发现子级如果需要添加图片,文字和其他自定义的扩展,这个方法不是很合适,只能做元素只有一个子级。(不知道我说的对不对,希望有新用法的朋友给我补充下)
相关文章推荐
- 【Audio】Audio对象的方法和属性
- 深入解析PHP的Yii框架中的缓存功能
- tomcat和jboss地址栏显示图标
- 汇编初始化SDRAM详解1~2
- 注册界面图形验证码,防攻击(网站、短信)
- 代码片段
- 树莓派实现NAS家庭服务器(流媒体播放、文件共享及下载机)
- man bind
- 线段树
- CodeForces - 368A Sereja and Coat Rack (水)
- keyup mouseout事件,pastes鼠标右键粘贴事件
- 在Windows下搭建Redmine
- 再议重写(override)后的super
- caffe中的iteration,batch_size, epochs理解
- Praat脚本提取时长及基频并示例如何绘制声调图
- c#命名规则和代码规则
- 20160328作业
- setValue:forKey与setObject:forKey的差异
- hdoj1495非常可乐《bfs》
- Java 1.8 ASM ClassReader failed to parse class file - probably due to a new Java class file version