如何在recyclerview适配器中使用视图绑定

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

视图绑定是一项功能,使您可以更轻松地编写与视图交互的代码。 在模块中启用视图绑定后,它将为该模块中存在的每个XML布局文件生成一个绑定类。 绑定类的实例包含对在相应布局中具有ID的所有视图的直接引用。

Advantages of view binding

视图绑定的优点

  • Null safety: Since view binding creates direct references to views, there’s no risk of a null pointer exception due to an invalid view ID. Additionally, when a view is only present in some configurations of a layout, the field containing its reference in the binding class is marked with @Nullable.

    空安全性:由于视图绑定会创建对视图的直接引用,因此不会因无效的视图ID而导致空指针异常的风险。 另外,当视图仅在布局的某些配置中存在时,在绑定类中包含其引用的字段将用@Nullable标记。

  • Type safety: The fields in each binding class have types matching the views they reference in the XML file. This means that there’s no risk of a class cast exception.

    类型安全性:每个绑定类中的字段具有与其在XML文件中引用的视图匹配的类型。 这意味着没有类强制转换异常的风险。

Adding view binding to your new or existing project

将视图绑定添加到新项目或现有项目

View binding is enabled on a module by module basis. To enable view binding in a module, set the view binding build option to true in the module-level build.gradle file, as shown in the example below:

视图绑定是逐模块启用的。 要在模块中启用视图绑定,请在模块级build.gradle文件中将view binding build选项设置为true,如下例所示:

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

Simple RecyclerView adapter with view Binding

具有视图绑定的简单RecyclerView适配器

class HoursAdapter(private val hoursList: List<HoursItem>)
    :RecyclerView.Adapter<HoursAdapter.HoursViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HoursViewHolder {
        val binding = HoursListItemsBinding
            .inflate(LayoutInflater.from(parent.context), parent, false)
        return HoursViewHolder(binding)
    }


    override fun getItemCount() = hoursList.size


    override fun onBindViewHolder(holder: HoursViewHolder, position: Int) {
        with(holder){
            with(hoursList[position]) {
                binding.topLearnerName.text = name
                val hours = "$hours learning hours, $country"
                binding.topLearnerTime.text = hours
                GlideApp.with(holder.itemView.context)
                    .load(badgeUrl)
                    .into(binding.topLearnerImage)


                holder.itemView.setOnClickListener {
                    Toast.makeText(holder.itemView.context, hours,
                        Toast.LENGTH_SHORT).show()
                }
            }
        }
    }


    inner class HoursViewHolder(val binding: HoursListItemsBinding)
        :RecyclerView.ViewHolder(binding.root)


}
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:cardElevation="8dp"
    app:cardCornerRadius="5dp"
    android:layout_marginTop="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginEnd="12dp">




    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">


    <ImageView
        android:id="@+id/topLearnerImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="16dp"
        android:src="@drawable/top_learner"
        android:contentDescription="@string/top_learner_icon"/>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@id/topLearnerImage"
            android:layout_marginStart="12dp">


            <TextView
                android:id="@+id/topLearnerName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Emeka John"
                android:textStyle="bold"
                android:textSize="16sp" />


            <TextView
                android:id="@+id/topLearnerTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="223, learning hours Nigeria"
                android:layout_marginTop="4dp"/>


        </LinearLayout>




    </RelativeLayout>


</com.google.android.material.card.MaterialCardView>

for more usage of view binding, use the link below

有关视图绑定的更多用法,请使用下面的链接

https://developer.android.com/topic/libraries/view-binding

https://developer.android.com/topic/libraries/view-binding

翻译自: https://medium.com/@daveson/how-to-use-view-binding-in-recyclerview-adapter-f818b96c678a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值