ExpandableListView总结

发布时间:2020-06-21 08:59:01 作者:一剑围城
来源:网络 阅读:405

在实现类似通信录,等带有两层或多层组织架构的列表功能时,会使用到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类似

 

SimpleCursorTreeAdapter

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

 


推荐阅读:
  1. 如何让ExpandableListView点击不收缩
  2. android的ExpandableListView

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

listview expandable lis

上一篇:数字资产多币种钱包系统开发安全如何做

下一篇:连root也干不掉的文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》