我是Kotlin和DataBinding的新手,基于Google I/O Android App on Github,我编写了一个可以工作的适配器,但是我无法完全理解为什么有人会比传统方法更喜欢DataBinding?在这种情况下有优势吗?:
internal class RecyclerAdapterKt : RecyclerView.Adapter<BaseViewHolder>() {
private val data = ArrayList<Item>()
...
/* with data binding */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
val inflater = LayoutInflater.from(parent.context)
return BaseViewHolder.BasicItemViewHolder(ItemViewBinding.inflate(inflater, parent, false))
}
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
val item = data[position]
(holder as BaseViewHolder.BasicItemViewHolder).bind(item)
}
// without data binding
// override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
// val view = from(parent.context).inflate(R.layout.item_view, parent, false)
// return BaseViewHolder(view)
// }
//
// override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
// val item = data[position]
// holder.tv_title.text = item.title
// holder.tv_message.text = item.message
// }
...
sealed class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
class BasicItemViewHolder(
val binding: ItemViewBinding
) : BaseViewHolder(binding.root) {
fun bind(item : Item) {
binding.tvTitle.text = item.title
// Difference with using itemView.tv_title.text?
binding.tvMessage.text = item.message
}
}
}
为什么不通过itemView在fun bind()中直接访问布局视图?
答案 0 :(得分:0)
实际上,它将减少ViewHolder绑定功能中的许多样板代码。诀窍在于Viewholder的XML和bind函数
数据绑定到位后,您的ViewHolder类将紧随其后
class BaseViewHolder(private val itemViewBinding: ItemViewBinding): RecyclerView.ViewHolder(itemViewBinding.root) {
fun bind(item: Item) {
itemViewBinding.item = item
itemViewBinding.executePendingBindings()
}
}
假设在进行数据绑定之前,您的XML文件item_view.xml如下
<LinearLayout 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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTitle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMessage/>
</LinearLayout>
数据绑定后,您的XML文件item_view.xml将如下所示
<layout>
<data>
<variable name="item"
type="com.sample.Item"/>
<!--your Item model should be defined in the packagename com.sample-->
</data>
<LinearLayout 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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTitle"
android:text="@{item.title}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMessage"
android:text="@{item.message}"/>
</LinearLayout>
因此,无论我们在Item模型类中添加多少个属性,bind函数都将相同。因此,优点是减少样板代码。