BaseRecyclerViewAdapterHelper之BaseMultiItemQuickAdapter实现多级列表功能

这里实现的是一个三级列表效果,可展开和收起。

  • 三个实体类ExpandItem、Expand1Item和Expand2Item,均要继承AbstractExpandableItem,实现接口MultiItemEntity.

public class ExpandItem extends AbstractExpandableItem implements MultiItemEntity {

    private String title;

    public ExpandItem(String title){
        this.title = title;
    }

    @Override
    public int getLevel() {
        return 0;
    }

    @Override
    public int getItemType() {
        return QuickExpandableAdapter.TYPE_LEVEL_0;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
public class Expand1Item extends AbstractExpandableItem implements MultiItemEntity {

    private String title;

    public Expand1Item(String title){
        this.title = title;
    }

    @Override
    public int getLevel() {
        return 1;
    }

    @Override
    public int getItemType() {
        return QuickExpandableAdapter.TYPE_LEVEL_1;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
public class Expand2Item implements MultiItemEntity {

    private String title;

    public Expand2Item(String title){
        this.title = title;
    }

    @Override
    public int getItemType() {
        return QuickExpandableAdapter.TYPE_LEVEL_2;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

  • 一个适配器QuickExpandableAdapter,继承自BaseMultiItemQuickAdapter。

public class QuickExpandableAdapter extends BaseMultiItemQuickAdapter, BaseViewHolder> {

    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    public static final int TYPE_LEVEL_2 = 2;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public QuickExpandableAdapter(List data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.view_expand_0);
        addItemType(TYPE_LEVEL_1, R.layout.view_expand_1);
        addItemType(TYPE_LEVEL_2, R.layout.view_expand_2);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()){
            case TYPE_LEVEL_0:
                final ExpandItem item0 = (ExpandItem) item;
                helper.setText(R.id.expand_list_item0_tv,item0.getTitle());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (item0.isExpanded()){
                            collapse(pos);
                            ToastUtils.showShortToast("收起:" + item0.getTitle());
                        }else {
                            expand(pos);
                            ToastUtils.showShortToast("展开:" + item0.getTitle());
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                final Expand1Item item1 = (Expand1Item) item;
                helper.setText(R.id.expand_list_item1_tv,item1.getTitle());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (item1.isExpanded()){
                            collapse(pos, false);
                            ToastUtils.showShortToast("收起:" + item1.getTitle());
                        } else {
                            expand(pos, false);
                            ToastUtils.showShortToast("展开:" + item1.getTitle());
                        }
                    }
                });
                break;
            case TYPE_LEVEL_2:
                final Expand2Item item2 = (Expand2Item) item;
                helper.setText(R.id.expand_list_item2_tv,item2.getTitle());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtils.showShortToast("点击:" + item2.getTitle());
                    }
                });
                break;
        }
    }
}


  • 实现类QuickExpandableActivity

public class QuickExpandableActivity extends BaseRecyclerViewActivity {

    private QuickExpandableAdapter expandableAdapter;

    @Override
    protected int getLayoutResId() {
        return super.getLayoutResId();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        titleNameTv.setText("实现多级列表功能");

        expandableAdapter = new QuickExpandableAdapter(getExpandListData(10));
        expandableAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        recyclerView.setAdapter(expandableAdapter);

        expandableAdapter.expandAll(0,true);

    }

    @Override
    protected void onRefreshComplete() {
        expandableAdapter.addData(0,getExpandListData(2));
        recyclerView.scrollToPosition(0);
    }

    private List getExpandListData(int count) {
        int lvCount = count;
        int lv1Count = 3;
        int lv2Count = 5;

        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);
        }

        return data;
    }
}
效果图如下:
BaseRecyclerViewAdapterHelper之BaseMultiItemQuickAdapter实现多级列表功能_第1张图片

你可能感兴趣的:(Android)