ExpandableListView总结-创新互联

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

创新互联公司长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为路北企业提供专业的成都网站设计、成都网站制作,路北网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

关键类:

1、SimpleExpandableListAdapter

2、SimpleCursorTreeAdapter

3、BaseExpandableListAdapter

4、ExpandableListActivity

SimpleExpandableListAdapter

构造方法:

SimpleExpandableListAdapter(Context context,

List> groupData, //组数据

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

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

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

List>> childData,//子元素数据

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

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

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

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

Map map1 = new HashMap<>();

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

集合保存所有的组数据

List> groups = new ArrayList<>();

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

Map cMap1 = new HashMap<>();

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

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

保存一个组的子元素数据

List> child1 = new ArrayList<>();

保存所有组的子元素数据

List>> 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>,SimpleCursorTreeAdapter数据源来自访问数据库返回的Cursor。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:ExpandableListView总结-创新互联
文章链接:http://lszwz.com/article/cecicp.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款