ExpandableListView使用解析(三级列表的实现)

ExpandableListView 使用解析(三级列表的实现)

在往常的设计中,往往有类似于QQ的二级列表的样式,而使用ExpandableListView变可以实现这种类似的效果。

当然,如果ExpandableListView嵌套ExpandableListView便可以实现三级列表,甚至多级条目,而本博客最终的实现效果便是三级条目。

首先看一下效果:

这里写图片描述

当然样式比较丑,这里只实现基本的逻辑。具体的样式可以根据自己的需要进行修改

ExpendableListView 的基本使用

万丈高楼平地起,想要实现三级列表肯定要从最简单的二级列表实现。下面就开始实现他的二级列表。

ExpendableListView的使用和ListView几乎一样:

  • 编写布局文件并查找ExpendableListView
  • 自定义适配器继承BaseExpandableListAdapter
  • setAdapter设置适配器。

下面开始进行实现:

  • 编写布局文件并查找控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ExpandableListView
        android:id="@+id/tree_view_simple"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >
    </ExpandableListView>


</LinearLayout>
  • 自定义ClassesExpandableListViewAdapter继承BaseExpandableListAdapter

在编写该类之前,我们必须要有我们的实体类。看一下我们定义的班级实体类Classes

/**
 *
 * 班级实体类
 * Created by MH on 2016/6/16.
 */
public class Classes {
   


    // 班级名
    public String name;


    // 班级中的学生列表
    public List<String> students;

}

ok,直接上ClassesExpandableListViewAdapter的代码,后面解释。

/**
 *
 * 班级的适配器
 * Created by MH on 2016/6/16.
 */
public class ClassesExpandableListViewAdapter extends BaseExpandableListAdapter {
   


    // 班级的集合
    private List<Classes> classes;

    // 创建布局使用
    private Activity activity;


    public ClassesExpandableListViewAdapter(List<Classes> classes, Activity activity) {
        this.classes = classes;
        this.activity = activity;
    }

    @Override
    public int getGroupCount() {
        // 获取一级条目的数量  就是班级的大小
        return classes.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // 获取对应一级条目下二级条目的数量,就是各个班学生的数量
        return classes.get(groupPosition).students.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // 获取一级条目的对应数据  ,感觉没什么用
        return classes.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        
  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值