二级列表

一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。

这个 ExpandableListAdapter 又是什么呢?和 ListView 使用的 BaseAdapter 差不多,都是用来给 View 提供数据、 实例化子布局的。

1:定义必要的布局
<ExpandableListView
android: id= "@+id/expand_list"
android:layout_width= "match_parent"
android:layout_height= "match_parent" />
2:定义要显示的数据,分组的数据是一个一维数组,子列表是一回合二维数组。
这边给打个样:group=   String[] {"A1","A2","A3","A4"};  这是一维数组就是我们的分组明,   childString=String [][]{{"B1"},{"B2"},{"B3"},{"B4"}};这是我们的子列表

安卓无非就3大元素 1:UI           2:Data 数据源       3:适配器
既让我们已经有了   数据源    只需要用我们的 数据源绑定到适配器展示到UI就OK了;


3:定义 俩个布局 一个是 分组的布局,和子列表的布局用TextView就Ok因为我们就定义的文字比较简单就不展示代码了
4:定义适配器               

        此适配器需要继承 BaseExpandableListAdapter  此抽象类,并重写相关的方法,10个方法    

        
    这边解释 重写的 10个方法的 意思   
    
 
 @Override//获取分组的个数
    public int getGroupCount() {
        return group.length;
    }
    @Override//获取指定分组中子选项的个数
    public int getChildrenCount(int groupPosition) {
        return childString[groupPosition].length;
    }
    @Override //获取指定的分组数据
    public Object getGroup(int groupPosition) {
        return group[groupPosition];
    }
    @Override//获取自选项中子选项的数据
    public Object getChild(int groupPosition, int childPosition) {
        return childString[groupPosition][childPosition];
    }
    @Override //获取组的id
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @Override//获取子选项的id
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override//分组和自选项释放稳定的id
    public boolean hasStableIds() {
        return true;
    }
    @Override//获取分组的视图
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder;
        if (convertView == null) {
            convertView =View.inflate(context,R.layout.group,null);
            groupHolder=new GroupHolder();
            groupHolder.textView=convertView.findViewById(R.id.tv_group);
            convertView.setTag(groupHolder);
        }else{
            groupHolder= (GroupHolder) convertView.getTag();
        }
        groupHolder.textView.setText(group[groupPosition]);
        return convertView;
    }
    @Override//获取子选项的视图
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder;
        if (convertView == null) {
            convertView =View.inflate(context,R.layout.child,null);
            childHolder=new ChildHolder();
            childHolder.textView=convertView.findViewById(R.id.tv_child);
            convertView.setTag(childHolder);
        }else{
            childHolder= (ChildHolder) convertView.getTag();
        }
        childHolder.textView.setText(childString[groupPosition][childPosition]);
        return convertView;
    }
    @Override//指定位置上的子元素是否可以选中
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupHolder{
        TextView textView;
    }
    class ChildHolder{
        TextView textView;
    }

这就是咋们重写的个方法的意思
剩下的就是把咋们的适配器和适配器绑定一下就OK

ListView类.setAdapter(new 适配器类);

好了到现在咋们的二级列表就讲完了。

猜你喜欢

转载自blog.csdn.net/qq_42046338/article/details/80175075