Android ViewBinding + BaseQuickAdapter结合使用

本文介绍了如何将Android的ViewBinding技术与第三方库BaseQuickAdapter结合,用于RecycleView的实现。通过四步详细讲解,包括创建BaseViewBindingAdapter基类、BaseViewBindingHolder、布局文件adapter_home_post_item_view.xml以及特定的AdapterHomePostItemViewBinding,最后展示如何使用自定义的HomeAdapter。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ViewBinding 和 RecycleView 结合使用,废话不多说,直接上代码

首先本人使用的是 第三方库 的baseAdapter :

com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.6

第一步,我们新建一个BaseViewBindingAdapter 基类

/**
 * @author Create by 17474 on 2021/4/29.
 * Email: [email protected]
 * Describe:使用ViewBinding 封装
 * 使用泛型 VH 指定类型为 BaseViewHolder
 */
public abstract class BaseViewBindingAdapter<T, VH extends BaseViewHolder> extends BaseQuickAdapter<T, VH> {

    public BaseViewBindingAdapter(int layoutResId, @Nullable List<T> data) {
        super(layoutResId, data);
    }

    public BaseViewBindingAdapter(int layoutResId) {
        super(layoutResId);
    }

    @NotNull
    @Override
    protected VH onCreateDefViewHolder(@NotNull ViewGroup parent, int viewType) {
        return  getViewBinding(viewType,LayoutInflater.from(getContext()),parent);
    }
    protected abstract VH getViewBinding(int viewType, LayoutInflater from, ViewGroup parent);
    
}

第二步,我们创建BaseViewBindingHolder

/**
 * @author Create by 17474 on 2021/4/29.
 * Email: [email protected]
 * Describe:
 */
public class BaseViewBindingHolder <VB extends ViewBinding> extends BaseViewHolder {
    public VB viewBind;

    public BaseViewBindingHolder(VB viewBind) {
        super(viewBind.getRoot());
        this.viewBind = viewBind;
    }

    public VB getViewBind() {
        return viewBind;
    }
}

第三步 创建一个 adapter_home_post_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/middle_layout_margin_default"
    app:cardBackgroundColor="@color/layout_bg"
    app:cardCornerRadius="@dimen/layout_corner_default"
    app:cardElevation="@dimen/layout_elevation_default">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值