ExpandableListView总结

在实现类似通信录,等带有两层或多层组织架构的列表功能时,会使用到ExpandableListView,他是ListView的子类,使用方式也和ListView大同小异,这里做一个总结。

关键类:

1、SimpleExpandableListAdapter

2、SimpleCursorTreeAdapter

3、BaseExpandableListAdapter

4、ExpandableListActivity

SimpleExpandableListAdapter

构造方法:

SimpleExpandableListAdapter(Context context,

List<? extends Map<String, ?>> groupData, //组数据

int groupLayout,//组数据使用的布局文件

String[] groupFrom, //组数据所使用的key数组

int[] groupTo,//组数据放置的控件id,例如TextView

List<? extends List<? extends Map<String,?>>> childData,//子元素数据

int childLayout, //子元素使用的布局文件

String[] childFrom, //子元素数据所使用的key数组

int[] childTo)//子元素数据放置的控件id,例如TextView

例如:生成一条组数据,组名为“group1”

Map<String, String> map1 = new HashMap<>();

map1.put("group", "group1");//其中"group"只是一个索引key

集合保存所有的组数据

List<Map<String, String>> groups = new ArrayList<>();

保存一条子元素数据,和组数据一样

Map<String, String> cMap1 = new HashMap<>();

cMap1.put("child","child1_data1");

child1.add(cMap1);//添加到集合

保存一个组的子元素数据

List<Map<String, String>> child1 = new ArrayList<>();

保存所有组的子元素数据

List<List<Map<String, String>>> childs = new ArrayList<>();

构造方法

SimpleExpandableListAdapter adapter1 = new SimpleExpandableListAdapter(this,

groups, R.layout.expandable_group, new String[]{"group"}, new int[]{R.id.text_group},

childs, R.layout.expandable_child, new String[]{"child"}, new int[]{R.id.text_child});

BaseExpandableListAdapter

继承BaseExpandableListAdapter实现自己的Adapter:

public class MyAdapter extends BaseExpandableListAdapter{

    String[] groups = new String[]{"Group_111","Group_222","Group_333"};
    String[][] childs = new String[][]{
            {"g1_c1","g1_c2","g1_c3"},
            {"g2_c1","g2_c2","g2_c3"},
            {"g3_c1","g3_c2","g3_c3"}};

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;//这里直接返回组元素下标
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;//这里直接返回第二层元素下标
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = View.inflate(MainActivity.this, R.layout.expandable_group, null);
        TextView text = (TextView)view.findViewById(R.id.text_group);
        text.setText(groups[groupPosition]);
        return view;//实际项目中应该考虑convertView的重用
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = View.inflate(MainActivity.this, R.layout.expandable_child, null);
        TextView text = (TextView)view.findViewById(R.id.text_child);
        text.setText(childs[groupPosition][childPosition]);
        return view;
    }

    @Override//组合子元素是否拥有稳定的id,底层的数据集改表是否影响该id
    public boolean hasStableIds() {
        return false;
    }

    @Override//子元素是否可选中,有点击反馈
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

ExpandableListActivity和ListActivity类似

  • 写自己的Activity继承自该类
  • 该类自带一个ExpandableListView。id为:“@android:id/list”(或者代码中为“list”)
  • 可以有一个id为:“@android:id/empty”布局,作为adapter没数据时的替代布局
  • 通常写自己的布局并满足上述条件,并加以扩展实现自己的需求

SimpleCursorTreeAdapter

不同于SimpleExpandableListAdapter数据源来自List<Map<?,?>>,SimpleCursorTreeAdapter数据源来自访问数据库返回的Cursor。

时间: 2024-12-14 04:58:35

ExpandableListView总结的相关文章

Android ExpandableListView 带有Checkbox的简单应用

expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height=&qu

Android ExpandableListView的简单应用

Expandablelistview1Activity.java package com.wangzhu.demoexpandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widg

android的ExpandableListView

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_

ExpandableListView实现展开更多和收起更多

[需求]: 如上面图示 当点开某个一级菜单的时候,其他菜单收起: 子级菜单默认最多5个: 多于5个的显示"展开更多" 点击"展开更多",展开该级所有子级菜单,同时显示"收起更多" [代码]: @Bind(R.id.exp_listview)ExpandableListView expListview; adapter = new MyAdapter1(dataBeans);expListview.setDividerHeight(0);expLi

Android常用控件之GridView与ExpandableListView的用法

概述 1.GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数. 2.ExpandableListView:与ListView相比,可以让每一列单元都拥有子列表. 内容 GridView 显示3列和多行的图片以及名称 布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sc

替换ExpandableListView右边箭头Group Indicator(小图标)

 先在drawable目录下建立xml文件: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_expanded="true" android:drawable="@drawab

Android ExpandableListView实例Demo

前几篇文章介绍了Listview,但在实际开发中也经常会用到多层的Listview来展示数据,比如qq中的好友展示,所以这张来了解一下ExpandableListview,基本思想与Listview大致是相同的,所以用起来会比较方便. 实现效果图: 程序代码: 布局文件: activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools

android ExpandableListView三级菜单的使用

由于本人所作的项目需要用到这种列表式的收缩与展开,因此,就好好研究了有关这方面的一些知识,当然,也借鉴了网上一些成功的案列.下面就是我模拟测试的一个展示界面. 实现上面的这些功能,我主要是通过ExpandableListView这一控件,以及BaseExpandableListAdapter适配器.这两者关联实现的.好的,那接下来,就对这些进行详细的展示. 所有的xml布局展示 ## activity_main.xml## <?xml version="1.0" encoding

android-基础编程-ExpandableListview

ExpandableListView继承ListView,具有LIstVIew的基本功能.此外具有group/child,由组与子元素组成. 1.布局主要有是三个. a.主布局: <ExpandableListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/expandlistview" androi

Android中使用ExpandableListView实现好友分组

一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源. 2.数据源,就是此处的适配器类,此方法继承了BaseExpandableListAdapter,它是ExpandableListView的一个子类.需要重写里面的多个方法.方法的意思,代码中都有详细的注释.数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式.get