BaseQuickAdapter使用(brvah实现多级列表)

BaseQuickAdapter特点

  • 可以减少重复代码
  • 添加Item点击事件,长按事件以及子控件的点击事件
  • 添加头部、尾部、下拉刷新、上拉加载涵盖多种动画处理,没有更多数据处理
  • 添加Item滑动动画。多种动画切换.
  • 添加新增删除Item动画
  • 网格、列表、流式布局随意切换
  • 添加空布局(列表无数据时,显示更加友好)
  • 拖拽和侧滑删除
  • 支持多类型布局
  • 字母导航

添加依赖

app内build.gradle
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

app外build.gradle
maven { url "https://jitpack.io" }

recyclerView适配器继承BaseQuickAdapter

public class TestAdapter extends BaseQuickAdapter {
    
    public TestAdapter(int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        
    }
}

我一般会将构造方法内的layoutResId去掉,直接填入布局XML

public TestAdapter(@Nullable List data) {
   super(R.layout.item_one, data);
}

设置显示内容

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.content,item);
    }

点击事件

        testAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this,"点击",Toast.LENGTH_SHORT).show();
            }
        });

在适配器中为item内子控件添加点击事件

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.addOnClickListener(R.id.content);
    }

使用方法

        testAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.content:
                        Toast.makeText(MainActivity.this, "点击", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

设置新数据

testAdapter.setNewData(multiItemEntityList);

刷新列表

testAdapter.notifyDataSetChanged();

利用BaseQuickAdapter来实现多级列表效果

BaseQuickAdapter使用(brvah实现多级列表)_第1张图片
树形列表
  • 实体类继承AbstractExpandableItem

注:AbstractExpandableItem 泛型填入的是二级菜单的实体类

public class OneBean extends AbstractExpandableItem implements MultiItemEntity {

    private String content;

    public OneBean(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

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

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

若不存在第三级子菜单,二级子菜单就不用继承AbstractExpandableItem

public class TwoBean implements MultiItemEntity {
    private String content;

    public TwoBean(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public int getItemType() {
        return TestAdapter.TYPE_TWO;
    }
}
  • 装入数据

    private void initData() {
        List multiItemEntityList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            OneBean oneBean = new OneBean("一级标题" + i);
            for (int j = 0; j < 3; j++) {
                oneBean.addSubItem(new TwoBean("二级标题" + j));
            }
            multiItemEntityList.add(oneBean);
        }
        testAdapter.setNewData(multiItemEntityList);
        testAdapter.notifyDataSetChanged();
    }
  • 如何打开和收起子菜单?

BaseQucikAdapter中有两个方法collapse、expand分别对应收起和打开。

                final OneBean oneBean = (OneBean) item;
                helper.setText(R.id.content, oneBean.getContent());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = helper.getAdapterPosition();
                        if (oneBean.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });

isExpanded判断当前状态

  • 打开所有列表

testAdapter.expandAll();

项目地址:github

你可能感兴趣的:(BaseQuickAdapter使用(brvah实现多级列表))