BaseRecyclerViewAdapterHelper库的基本使用方法

作者: wxyass 分类: Android 发布时间: 2017-10-30 15:29

Github引入
https://github.com/CymChad/BaseRecyclerViewAdapterHelper

使用文档
http://www.jianshu.com/p/b343fcff51b0


具体使用步骤:

1 创建bean对象类 implements MultiItemEntity

public class ListBean implements MultiItemEntity {

    private int mItemType = 0;
    private String mImageUrl = null;
    private String mText = null;
    private String mValue = null;
    private int mId = 0;
    private LatteDelegate mDelegate = null;
    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = null;

    public ListBean(int mItemType, String mImageUrl, String mText, String mValue, int mId, LatteDelegate mDelegate, CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mItemType = mItemType;
        this.mImageUrl = mImageUrl;
        this.mText = mText;
        this.mValue = mValue;
        this.mId = mId;
        this.mDelegate = mDelegate;
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

    public String getImageUrl() {
        return mImageUrl;
    }

    public String getText() {
        if (mText == null) {
            return "";
        }
        return mText;
    }

    public String getValue() {
        if (mValue == null) {
            return "";
        }
        return mValue;
    }

    public int getId() {
        return mId;
    }

    public LatteDelegate getDelegate() {
        return mDelegate;
    }

    public CompoundButton.OnCheckedChangeListener getmOnCheckedChangeListener() {
        return mOnCheckedChangeListener;
    }

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

    public static final class Builder {

        private int id = 0;
        private int itemType = 0;
        private String imageUrl = null;
        private String text = null;
        private String value = null;
        private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = null;
        private LatteDelegate delegate = null;

        public Builder setId(int id) {
            this.id = id;
            return this;
        }

        public Builder setItemType(int itemType) {
            this.itemType = itemType;
            return this;
        }

        public Builder setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
            return this;
        }

        public Builder setText(String text) {
            this.text = text;
            return this;
        }

        public Builder setValue(String value) {
            this.value = value;
            return this;
        }

        public Builder setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
            this.onCheckedChangeListener = onCheckedChangeListener;
            return this;
        }

        public Builder setDelegate(LatteDelegate delegate) {
            this.delegate = delegate;
            return this;
        }

        public ListBean build() {
            return new ListBean(itemType, imageUrl, text, value, id, delegate, onCheckedChangeListener);
        }
    }
}

需要实现的是库中的MultiItemEntity类,

其中 getItemType() 返回int值是: 条目布局类型

在写adapter适配器时,不同条目布局对应不同类型值 (当创建实体类对象时,写入的条目类型值,与adapter中的条目类型值一致时,才能将实体类数据对应到相应条目布局上)

要注意的是:

一个adapter可以写入多个条目布局(在其构造中使用addItemType方法), 当通过网络获取多个实体类对象时,这些对象中有的字段有值,有的字段没值,比如: 对象1是展示一张图片,对象2是展示一句话,对象3是展示一个广告轮播图, 不同的对象需要用不同的条目布局来展示, 在adapter中就需要设定int值来对应这些不同的条目布局.

而我们在创建实体类对象时,也在对象中写入这个字段值,这样不同的对象通过这个值对应展示到不同的条目布局上.

2 创建一个类

定义不同的int值, 将对应不同的条目布局

public class ListItemType {
    public static final int ITEM_NORMAL = 20;// 条目布局1
    public static final int ITEM_AVATAR = 21;// 条目布局2
    public static final int ITEM_SWITCH = 22;// 条目布局3
}   

3 将 json 转成 list 集合

// 这里自定义不同对象,传入不同的条目类型值
final ListBean address = new ListBean.Builder()
        .setItemType(ListItemType.ITEM_NORMAL)
        .setId(1)
        .setDelegate(new AddressDelegate())
        .setText("收货地址")
        .build();

final ListBean system = new ListBean.Builder()
        .setItemType(ListItemType.ITEM_NORMAL)
        .setId(2)
        .setDelegate(new SettingsDelegate())
        .setText("系统设置")
        .build();

final ListBean image = new ListBean.Builder()
        .setItemType(ListItemType.ITEM_AVATAR)
        .setId(3)
        .setImageUrl("http://i9.qhimg.com/t017d891ca365ef60b5.jpg")
        .build();

final ListBean name = new ListBean.Builder()
        .setItemType(ListItemType.ITEM_NORMAL)
        .setId(4)
        .setText("姓名")
        .setDelegate(new NameDelegate())
        .setValue("未设置姓名")
        .build();

final ListBean gender = new ListBean.Builder()
        .setItemType(ListItemType.ITEM_NORMAL)
        .setId(5)
        .setText("性别")
        .setValue("未设置性别")
        .build();

final ListBean birth = new ListBean.Builder()
        .setItemType(ListItemType.ITEM_NORMAL)
        .setId(6)
        .setText("生日")
        .setValue("未设置生日")
        .build();            

final List<ListBean> data = new ArrayList<>();
data.add(address);
data.add(system);

4 创建adapter

这里继承BaseMultiItemQuickAdapter 其中两个泛型分别是<数据结构,holder>
在构造方法中将list集合传递进来,并初始化不同的条目布局
在convert() 方法中给每个条目填充数据

public class ListAdapter extends BaseMultiItemQuickAdapter<ListBean, BaseViewHolder> {

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate()
            .centerCrop();

    // 构造方法: 将list集合传递进来
    public ListAdapter(List<ListBean> data) {
        super(data);
        // 添加条目布局1
        addItemType(ListItemType.ITEM_NORMAL, R.layout.arrow_item_layout);
        // 添加条目布局2
        addItemType(ListItemType.ITEM_AVATAR, R.layout.arrow_item_avatar);
        // 添加条目布局3
        addItemType(ListItemType.ITEM_SWITCH,R.layout.arrow_switch_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, ListBean item) {
        switch (helper.getItemViewType()) {// 当前条目的布局类型
            case ListItemType.ITEM_NORMAL: // 布局1
                helper.setText(R.id.tv_arrow_text, item.getText());
                helper.setText(R.id.tv_arrow_value, item.getValue());
                break;
            case ListItemType.ITEM_AVATAR: // 布局2
                Glide.with(mContext)
                        .load(item.getImageUrl())
                        .apply(OPTIONS)
                        .into((ImageView) helper.getView(R.id.img_arrow_avatar));
                break;
            case ListItemType.ITEM_SWITCH: // 布局3
                helper.setText(R.id.tv_arrow_switch_text,item.getText());
                final SwitchCompat switchCompat = helper.getView(R.id.list_item_switch);
                switchCompat.setChecked(true);
                switchCompat.setOnCheckedChangeListener(item.getmOnCheckedChangeListener());
                break;
            default:
                break;
        }
    }
}     

5 为RecyclerView设置adapter

//设置RecyclerView
final LinearLayoutManager manager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(manager);
final ListAdapter adapter = new ListAdapter(data);
mRecyclerView.setAdapter(adapter);

6 设置RecyclerView的条目点击事件

mRecyclerView.addOnItemTouchListener(new PersonalClickListener(this));  

其中自定义PersonalClickListener类继承SimpleClickListener

public class PersonalClickListener extends SimpleClickListener {

    private final LatteDelegate DELEGATE;

    public PersonalClickListener(LatteDelegate delegate) {
        this.DELEGATE = delegate;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        final ListBean bean = (ListBean) baseQuickAdapter.getData().get(position);
        int id = bean.getId();
        switch (id) {
            case 1:
                DELEGATE.getParentDelegate().getSupportDelegate().start(bean.getDelegate());
                break;
            case 2:
                DELEGATE.getParentDelegate().getSupportDelegate().start(bean.getDelegate());
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {

    }
}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注