二级列表

二级列表(ExpandableListView)

  1. 概念:

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

一种用于垂直滚动展示二级列表的视图,组可以单独展开。这些选项的数据通过ExpandableListView关联。
  1. 使用场景:


    QQ.png
  2. 属性:

divider 设置父选项之间的分割线样式
childDivider 设置子选项之间的分割线样式
divider 设置分割线的高度
groupIndicator 父项前的图标
childIndicator 子项前的图标

  1. 步骤:
  1. 创建布局并找id
  2. 获取数据(死数据,网络数据)
  3. 创建视图(Group,item)
  4. 创建适配器(继承BaseExpandableListAdapter)
  5. 给二级列表绑定适配器

5.适配器(BaseExpandableListAdapter)

     //父项的个数
    @Override
    public int getGroupCount() {
        return list.size();
    }

     //某个子项的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getChildren().size();
    }

    //获取某个父项
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    //获取某个子项
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getChildren().get(childPosition);
    }

    //父项的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //子项的id 
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }

    //获取父项的view
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.layout_group, null);
        ViewHolder holder = new ViewHolder(convertView);
        holder.tv_group.setText(list.get(groupPosition).getName());
        return convertView;
    }

    //获取子项的view
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.layout_group, null);
        ViewHolder holder = new ViewHolder(convertView);
        holder.tv_group.setText(list.get(groupPosition).getChildren().get(childPosition).getName());
        return convertView;
    }

    //子项是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
  1. 监听事件 :适配器的isChildSelectable()方法必须返回true

setOnChildClickListener() 单击子选项
setOnGroupClickListener() 单击组选项
setOnGroupCollapseListener() 分组合并(关)
setOnGroupExpandListener() 分组合并(开)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容