BaseQuickAdapter与ViewBinding结合使用

BaseQuickAdapter基本用法

BaseQuickAdapter 就是一款为Android开发者打造的针对繁琐的适配器的构建的快速开源项目。

dependencies {
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.x'
}

新建RecyclerView Item布局item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="15sp"
        tools:text="文本文字" />
</LinearLayout>

Java

public class RecyclerAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    /**
     * 构造方法,此示例中,在实例化Adapter时就传入了一个List。
     * 如果后期设置数据,不需要传入初始List,直接调用 super(layoutResId); 即可
     */
    public RecyclerAdapter() {
        super(R.layout.item_view);
    }

    /**
     * 在此方法中设置item数据
     */
    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull String data) {
        helper.setText(R.id.tv_item, data);
    }
}

kotlin

class RecyclerAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_view) {
    override fun convert(holder: BaseViewHolder, data: String) {
        holder.setText(R.id.tv_item, data)
    }
}
Activity中设置:
RecyclerAdapter adapter = new RecyclerAdapter();
mRecyclerView.setAdapter(adapter);
 
 // 设置新的数据方法
adapter.setNewData(list)

BaseQuickAdapter与DataBinding用法

public class DataBindingAdapter extends BaseQuickAdapter<Movie, BaseViewHolder> {

    private MoviePresenter mPresenter = new MoviePresenter();

    public DataBindingAdapter() {
        super(R.layout.item_movie);
    }
        
    /**
     * 当 ViewHolder 创建完毕以后,会执行此回掉
     * 可以在这里做任何你想做的事情
     */
    @Override
    protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
        // 绑定 view
        DataBindingUtil.bind(viewHolder.itemView);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull Movie item) {
        if (item == null) {
            return;
        }

        // 获取 Binding
        ItemMovieBinding binding = helper.getBinding();
        if (binding != null) {
            // 设置数据
            binding.setMovie(item);
            binding.setPresenter(mPresenter);
            binding.executePendingBindings();
        }
    }
}

摘自github DataBinding使用

BaseQuickAdapter与ViewBinding用法

前面两种方式官方已经存在使用用法,对于ViewBinding自己对其封装了一个基类,RecyclerView的adapter继承BaseBindingAdapter即可快速对BaseQuickAdapter的使用。

class VBViewHolder<VB : ViewBinding>(val vb: VB, view: View) : BaseViewHolder(view)

abstract class BaseBindingAdapter<VB : ViewBinding, T>(data: MutableList<T>? = null) :
    BaseQuickAdapter<T, VBViewHolder<VB>>(0, data) {

    //重写返回自定义 ViewHolder
    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): VBViewHolder<VB> {
        //这里为了使用简洁性,使用反射来实例ViewBinding
        val vbClass: Class<VB> =
            (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
        val inflate = vbClass.getDeclaredMethod(
            "inflate",
            LayoutInflater::class.java,
            ViewGroup::class.java,
            Boolean::class.java
        )
        val mBinding =
            inflate.invoke(null, LayoutInflater.from(parent.context), parent, false) as VB
        return VBViewHolder(mBinding, mBinding.root)
    }
}

HomeItemCategoryItemCardViewBinding布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="110dp"
    android:layout_height="110dp">

    <ImageView
        android:id="@+id/iv_category_bg"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:background="@drawable/common_tools_iv_bg" />

    <com.quyunshuo.common.view.CommonCustomTextView
        android:id="@+id/tv_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="广告"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        app:layout_constraintBottom_toBottomOf="@id/iv_category_bg"
        app:layout_constraintEnd_toEndOf="@+id/iv_category_bg"
        app:layout_constraintStart_toStartOf="@id/iv_category_bg"
        app:layout_constraintTop_toTopOf="@+id/iv_category_bg" />

</androidx.constraintlayout.widget.ConstraintLayout>

//1、传入ViewBinding对象 2、传入数据类型
public class CategoryItemAdapter
        extends BaseBindingAdapter<HomeItemCategoryItemCardViewBinding, SquareCard> {

    @Override
    protected void convert(@NotNull VBViewHolder<HomeItemCategoryItemCardViewBinding> homeItemCategoryItemCardViewBindingVBViewHolder, SquareCard squareCard) {
        //获取绑定ViewHolder中的ViewBinding
        HomeItemCategoryItemCardViewBinding binding = homeItemCategoryItemCardViewBindingVBViewHolder.getVb();
        //设置textview值
        binding.tvLabel.setText(squareCard.getTitle());
    }
}

总结

封装一个基类之后,整个使用与之前完全一样,之前传入的是R.layout.layout_demo,现在传入的是ViewBinding对象,完结~~~

之前
public DemoAdapter(list List<String>) {
     super(R.layout.layout_demo, list);
}

之后
public class CategoryItemAdapter
        extends BaseBindingAdapter<HomeItemCategoryItemCardViewBinding, SquareCard> {
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容