Android 泽宇二级列表


在开发 Android APP 的时候,难免会需要实现二级列表的情况,而在自己的项目中使用的列表是ExpandableListView

如若转发标明转载处:https://mp.csdn.net/postedit/81059332

public class ErAdapter extends BaseExpandableListAdapter {
    List list;

    Context context;

    public ErAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    //一级列表的个数
    @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);
    }

    //一级列表对应的索引
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    //二级列表对应的索引
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //每个item是否固定
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //填充以及列表
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.zu,null);
            holder=new GroupHolder();
            holder.name=convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        }else {
            holder= (GroupHolder) convertView.getTag();
        }
        holder.name.setText(list.get(groupPosition).getName());
        return convertView;
    }

    //填充二级列表
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.lie,null);
            holder=new ChildHolder();
            holder.name=convertView.findViewById(R.id.name);
            holder.time=convertView.findViewById(R.id.time);
            holder.size=convertView.findViewById(R.id.size);
            convertView.setTag(holder);
        }else {
            holder= (ChildHolder) convertView.getTag();
        }
        holder.name.setText(list.get(groupPosition).getChildren().get(childPosition).getName());
        int partDuration = list.get(groupPosition).getChildren().get(childPosition).getPartDuration();
        int time = partDuration / 60;
        holder.time.setText(time+"分钟");
        holder.size.setText(list.get(groupPosition).getChildren().get(childPosition).getPartSize()+"M");
        return convertView;
    }

    //二级列表中每个能否被选中,如果有点击事件一定要设为true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupHolder{
        TextView name;
    }
    class ChildHolder{
        TextView name,time,size;
    }
}


二级列表对应的点击事件

ErAdapter erAdapter = new ErAdapter(result, getContext());
ex.setAdapter(erAdapter);
ex.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        callBack.listenter(groupPosition, childPosition);
        return true;
    }
});

你可能感兴趣的:(Android 泽宇二级列表)