ViewBinding and DataBinding DataBinding
The ViewBinding library generates an xxxLayoutbindingbinding.java file for each xxx_Layout.xml file (unless tools:viewBindingIgnore=”true” is declared), The member variables in this type of file contain all the controls in the XML that declare android:id. Unlike DataBinding, it is not intrusive with the original XML file, which requires the <layout> tag to wrap the original layout, so if you don’t want to introduce DataBinding, But if you want to simplify template code like findViewById, a ViewBinding is a great way to do it.
This article does not discuss how to use ViewBinding in activities and fragments, only discuss the use of RecyclerView Adapter
2. Enable view binding
// File name build.gradle (:app)
android {
...
buildFeatures {
viewBinding = true}}Copy the code
3. Packaging Adapter
// File name commonRvAdapter.kt
/ * * *@description: recyclerView Adapter simple encapsulation, only for single type viewType * *@paramE: List data entity class *@paramBinding class **/ for the XML file V: item
abstract class CommonRvAdapter<E : Any, V : ViewBinding> : RecyclerView.Adapter<CommonRvHolder<V>>() {
/** * Data source */
open var data: MutableList<E> = mutableListOf()
set(value) {
field = value
notifyItemRangeChanged(0, value.size)
}
// Why not wrap this method as well? Because I don't want to use reflection
abstract override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonRvHolder<V>
override fun getItemCount(a): Int = data.size
override fun onBindViewHolder(holder: CommonRvHolder<V>, position: Int) {
onBindViewHolder(holder, holder.adapterPosition, holder.binding, data[holder.adapterPosition])
}
abstract fun onBindViewHolder(holder: CommonRvHolder<V>, position: Int, binding: V, bean: E)
}
open class CommonRvHolder<V : ViewBinding>(val binding: V) : RecyclerView.ViewHolder(binding.root) {
}
Copy the code
4. Use cases
- Suppose you have a layout file, item_student.xml, with a single TextView that displays student names
<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="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv_student_name"
style="@style/text_2.body.dark"
android:layout_width="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Student name"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code
- Let’s say I have Student entity class student.kt
class Student {
var name: String = ""
}
Copy the code
- Important: Create Adapter based on Binding class and entity class
class StudentAdapter : CommonRvAdapter<Student, ItemStudentBinding>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonRvHolder<ItemStudentBinding> {
// Use the Binding class to parse the layout Settings into the Holder
val inflate = ItemStudentBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return CommonRvHolder(inflate)
}
override fun onBindViewHolder(holder: CommonRvHolder<ItemStudentBinding>, position: Int, binding: ItemStudentBinding, bean: Student) {
// The binding object contains all the controls, and the Bean object contains all the entity properties
binding.tvStudentName.text = bean.name
binding.root.setOnClickListener {
ToastUtils.showShort("You clicked${bean.name}Students")}}}Copy the code
- Fill in the data
val students = mutableListOf<Student>()
for (i in 0 until 100) {
students.add(Student().apply { name = "The classmate$i" })
}
adapter.data = students
Copy the code