Android:BaseMultiItemQuickAdapter打造嵌套/多级列表

 

效果图:

Android:BaseMultiItemQuickAdapter打造嵌套/多级列表_第1张图片

Android:BaseMultiItemQuickAdapter打造嵌套/多级列表_第2张图片

1 引入库

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'

2 对象bean

一级列表:

package com.lucas.yanfriends.model.YanNew.select;



import com.chad.library.adapter.base.entity.AbstractExpandableItem;

import com.chad.library.adapter.base.entity.MultiItemEntity;



import java.io.Serializable;



import static com.lucas.yanfriends.ui.selectSchoolMajor.adapter.MajorSmallAdapter.ITEM_FIRST_LEVEL;



public class MajorMainBean extends AbstractExpandableItem implements Serializable , MultiItemEntity {





    private String major_main_name;



    public MajorMainBean(String major_main_name) {

        this.major_main_name = major_main_name;

    }



    public String getMajor_main_name() {

        return major_main_name;

    }



    public void setMajor_main_name(String major_main_name) {

        this.major_main_name = major_main_name;

    }



    @Override

    public int getItemType() {

        return ITEM_FIRST_LEVEL;

    }



    @Override

    public int getLevel() {

        return 0;

    }

}

二级列表:

package com.lucas.yanfriends.model.YanNew.select;



import com.chad.library.adapter.base.entity.MultiItemEntity;



import java.io.Serializable;



import static com.lucas.yanfriends.ui.selectSchoolMajor.adapter.MajorSmallAdapter.ITEM_FIRST_LEVEL;

import static com.lucas.yanfriends.ui.selectSchoolMajor.adapter.MajorSmallAdapter.ITEM_SECOND_LEVEL;



public class MajorSmallBean implements Serializable , MultiItemEntity {





    private int major_id;

    private String major_name;

    private int major_bbsid;

    private int major_pid;

    private String major_pname;



    public int getMajor_id() {

        return major_id;

    }



    public void setMajor_id(int major_id) {

        this.major_id = major_id;

    }



    public String getMajor_name() {

        return major_name;

    }



    public void setMajor_name(String major_name) {

        this.major_name = major_name;

    }



    public int getMajor_bbsid() {

        return major_bbsid;

    }



    public void setMajor_bbsid(int major_bbsid) {

        this.major_bbsid = major_bbsid;

    }



    public int getMajor_pid() {

        return major_pid;

    }



    public void setMajor_pid(int major_pid) {

        this.major_pid = major_pid;

    }



    public String getMajor_pname() {

        return major_pname;

    }



    public void setMajor_pname(String major_pname) {

        this.major_pname = major_pname;

    }



    @Override

    public int getItemType() {

        return ITEM_SECOND_LEVEL;

    }

}

3 一/二级列表的布局

一级列表的布局:







    

        

        

        

    





二级列表的布局:







    

        

        

    



4 Adapter

package com.lucas.yanfriends.ui.selectSchoolMajor.adapter;



import android.support.annotation.Nullable;



import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;

import com.chad.library.adapter.base.BaseViewHolder;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import com.lucas.yanfriends.R;

import com.lucas.yanfriends.model.YanNew.select.MajorMainBean;

import com.lucas.yanfriends.model.YanNew.select.MajorSmallBean;



import java.util.List;





public class MajorSmallAdapter extends BaseMultiItemQuickAdapter {



    public static final int ITEM_FIRST_LEVEL = 1;

    public static final int ITEM_SECOND_LEVEL = 2;





    public MajorSmallAdapter(@Nullable List data) {

        super(data);



        addItemType(ITEM_FIRST_LEVEL, R.layout.list_main_major_item);

        addItemType(ITEM_SECOND_LEVEL, R.layout.list_small_major_item);

    }



    @Override

    protected void convert(BaseViewHolder helper, MultiItemEntity item) {

        switch (helper.getItemViewType()) {

            case ITEM_FIRST_LEVEL:

                final MajorMainBean item_temp = (MajorMainBean) item;

                helper.setText(R.id.tv_name, item_temp.getMajor_main_name());

                helper.itemView.setOnClickListener(v -> {

                    int pos = helper.getAdapterPosition();

                    if (item_temp.isExpanded()) {

                        collapse(pos);

                    } else {

                        expand(pos);

                    }

                });

                break;

            case ITEM_SECOND_LEVEL:

                final MajorSmallBean item_temp2 = (MajorSmallBean) item;

                helper.setText(R.id.tv_name, item_temp2.getMajor_name());

                break;

        }

    }





}

5 activty中的设置

5.1 适配器

​
//--------------------------列表适配器--------------------------

        //Adapter适配器初始化

        recycle.setLayoutManager(new LinearLayoutManager(mContext));

        //设置列表默认动画效果

        recycle.setItemAnimator(new DefaultItemAnimator());

        //绑定适配器

        majorSmallAdapter = new MajorSmallAdapter(list_MajorBean);

        recycle.setAdapter(majorSmallAdapter);

        //列表点击事件

        majorSmallAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {

            @Override

            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {



            }

        });

        //--------------------------适配器--------------------------

​

5.2 数据填充

for (MultiItemEntity multiItemEntity : list_MajorBean_beifen) {

                                MajorSmallBean majorSamllBean = ((MajorSmallBean) multiItemEntity);

                                if (temp_class != majorSamllBean.getMajor_pid()) {

                                    MajorMainBean majorMainBean = new MajorMainBean(majorSamllBean.getMajor_name());

                                    temp_class = majorSamllBean.getMajor_pid();

                                    for (MultiItemEntity multiItemEntity2 : list_MajorBean_beifen) {

                                        MajorSmallBean majorSamllBean2 = ((MajorSmallBean) multiItemEntity2);

                                        if (majorSamllBean2.getMajor_pid() == temp_class) {

                                            majorMainBean.addSubItem(majorSamllBean2);

                                        }

                                    }

                                    list_MajorBean.add(majorMainBean);

                                }

                            }



 

关于数据的构造,简化下来如下:

List data = new ArrayList<>();
        for (int i = 0; i < lvCount; i++) {
            ExpandItem item0 = new ExpandItem("一级列表标题" + i);
            for (int j = 0; j < lv1Count; j++) {
                Expand1Item item1 = new Expand1Item("二级列表标题" + j);
                for (int k = 0; k < lv2Count; k++) {
                    item1.addSubItem(new Expand2Item("三级列表标题" + k));
                }
                item0.addSubItem(item1);
            }
            data.add(item0);
        }

 

你可能感兴趣的:(Android)