使用ExpandableListView实现三级列表

 1.布局

<ExpandableListView
            android:id="@+id/elv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:scrollbars="none"
            android:groupIndicator="@null"/>

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:paddingBottom="15dp">

    <TextView
            android:id="@+id/adapter_parent_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:layout_centerVertical="true"
            android:maxLines="1"
            android:textColor="@color/color_333"
            android:textSize="16sp"/>
    <ImageView
            android:id="@+id/adapter_parent_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:background="@drawable/open_right"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
       >

    <ImageView
            android:id="@+id/adapter_child_cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_centerVertical="true"
            android:button="@null"/>

    <TextView
            android:id="@+id/adapter_child_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:text=""
            android:layout_toRightOf="@+id/adapter_child_cb"
            android:textColor="@color/color_333"
            android:textSize="14sp"
            android:layout_centerVertical="true"/>

    <ImageView
            android:id="@+id/adapter_child_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/open_right"
            android:layout_centerVertical="true"/>
</RelativeLayout>

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        >

    <TextView
            android:id="@+id/adapter_child_next_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="36dp"
            android:layout_marginRight="7.5dp"
            android:lineSpacingExtra="5.5dp"
            android:paddingBottom="10dp"
            android:textColor="@color/color_666"
            android:textSize="14sp" />
</RelativeLayout>

2.adapter设置:第一级的child数设置为1,child里使用ExpandableLIstView实现二级列表。即ExpandableLIstView嵌套ExpandableLIstView,只不过嵌套的那一个ExpandableLIstView要重写,解决嵌套之下显示不全的问题

public class ParentAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<SelectSubjectModel> mData;
    private List<String> dataStr;
    private List<String> selStr = new ArrayList<>();

    public ParentAdapter(Context context, List<SelectSubjectModel> data, List<String> dataStr) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.dataStr = dataStr;
        this.selStr.addAll(dataStr);
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).children.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder holder = null;
        if (convertView == null) {
            holder = new ParentHolder();
            convertView = mInflater.inflate(R.layout.adapter_parent, null);
            holder.tv = convertView.findViewById(R.id.adapter_parent_title);
            holder.img = convertView.findViewById(R.id.adapter_parent_img);
            convertView.setTag(holder);
        } else {
            holder = (ParentHolder) convertView.getTag();
        }
        holder.tv.setText(mData.get(groupPosition).name);
        if (isExpanded) {
            holder.img.setBackgroundResource(R.drawable.open_down);
        } else {
            holder.img.setBackgroundResource(R.drawable.open_right);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             final ViewGroup parent) {
        CustomExpandableListView view = (CustomExpandableListView) convertView;
        if (convertView == null) {
            view = new CustomExpandableListView(mContext);
            view.setDivider(null);
            view.setGroupIndicator(null);
        }

        SelectSubjectChildAdapter adapter = new SelectSubjectChildAdapter(mContext, mData.get(groupPosition).children);

        view.setAdapter(adapter);
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private OnCheckListener listener;

    public void setOnCheckListener(OnCheckListener listener) {
        this.listener = listener;
    }

    public interface OnCheckListener {
        public void onCheck(String id, boolean checked);
    }

    class ParentHolder {
        TextView tv;
        ImageView img;
    }

    class SelectSubjectChildAdapter extends BaseExpandableListAdapter {
        private Context mContext;
        private LayoutInflater mInflater;
        private List<SelectSubjectModel> mData;


        public SelectSubjectChildAdapter(Context context, List<SelectSubjectModel> data) {
            this.mContext = context;
            this.mInflater = LayoutInflater.from(context);
            this.mData = data;

        }

        @Override
        public int getGroupCount() {
            return mData.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return mData.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return mData.get(groupPosition).children.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

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

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            final SelectSubjectModel model = mData.get(groupPosition);
            ChildHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.adapter_select_subject_child, null);
                holder = new ChildHolder();
                holder.childCb = convertView.findViewById(R.id.adapter_child_cb);
                holder.childTv = convertView.findViewById(R.id.adapter_child_title);
                holder.childImg = convertView.findViewById(R.id.adapter_child_img);
                convertView.setTag(holder);
            } else {
                holder = (ChildHolder) convertView.getTag();
            }
            holder.childTv.setText(model.name);
            if (isExpanded) {
                holder.childImg.setBackgroundResource(R.drawable.open_down);
            } else {
                holder.childImg.setBackgroundResource(R.drawable.open_right);
            }
            if (model.checked) {
                holder.childCb.setBackgroundResource(R.drawable.folder_is_selected);
            } else {
                holder.childCb.setBackgroundResource(R.drawable.folder_not_selected);
            }
            final ChildHolder childHolder = holder;
            holder.childCb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (model.checked) {
                        model.checked = false;
                        childHolder.childCb.setBackgroundResource(R.drawable.folder_not_selected);
                        if (listener != null) {
                            listener.onCheck(model.code, model.checked);
                        }
                        selStr.remove(model.code);
                    } else {
                        if (selStr.size() == dataStr.size()) {
                            ToastMaster.showToast(mContext, "最多选择" + dataStr.size() + "个");
                        } else {
                            model.checked = true;
                            childHolder.childCb.setBackgroundResource(R.drawable.folder_is_selected);
                            if (listener != null) {
                                listener.onCheck(model.code, model.checked);
                            }
                            selStr.add(model.code);
                        }
                    }

                }
            });

            return convertView;
        }


        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                                 ViewGroup parent) {
            List<SelectSubjectModel> childModel = mData.get(groupPosition).children;
            ChildNextHolder nextHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.adapter_select_subject_childnext, null);
                nextHolder = new ChildNextHolder();
                nextHolder.childNextTv = convertView.findViewById(R.id.adapter_child_next_title);
                convertView.setTag(nextHolder);
            } else {
                nextHolder = (ChildNextHolder) convertView.getTag();
            }
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < childModel.size(); i++) {
                sb.append(childModel.get(i).name + "   ");
            }
            nextHolder.childNextTv.setText(sb.toString());
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }


        class ChildHolder {
            ImageView childCb;
            TextView childTv;
            ImageView childImg;
        }

        class ChildNextHolder {
            TextView childNextTv;
        }


    }
}

 3.重写的ExpandableListView

/**
 * 解决嵌套之下显示不全的问题
 */
public class CustomExpandableListView extends ExpandableListView {
    public CustomExpandableListView(Context context) {
        super(context);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 解决显示不全的问题
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
                , MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

4.效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值