-
-
Notifications
You must be signed in to change notification settings - Fork 440
04.Use ViewBinding And DataBinding
zhangpan edited this page Mar 7, 2023
·
6 revisions
class ViewBindingSampleAdapter(private val mRoundCorner: Int) : BaseBannerAdapter<Int>() {
override fun createViewHolder(
parent: ViewGroup,
itemView: View,
viewType: Int
): BaseViewHolder<Int> {
return ViewBindingViewHolder(ItemSlideModeBinding.bind(itemView))
}
override fun bindData(holder: BaseViewHolder<Int>, data: Int, position: Int, pageSize: Int) {
if (holder is ViewBindingViewHolder) {
holder.viewBinding.bannerImage.setRoundCorner(mRoundCorner)
holder.viewBinding.bannerImage.setImageResource(data)
}
}
override fun getLayoutId(viewType: Int): Int {
return layout.item_slide_mode
}
}
internal class ViewBindingViewHolder(var viewBinding: ItemSlideModeBinding) :
BaseViewHolder<Int>(viewBinding.root)
class DataBindingSampleAdapter : BaseBannerAdapter<BannerData?>() {
override fun createViewHolder(
parent: ViewGroup, itemView: View,
viewType: Int
): BaseViewHolder<BannerData?> {
val binding = DataBindingUtil.bind<ItemSlideModelDataBindingBinding>(itemView)
?: throw NullPointerException("binding is Null")
return DataBindingViewHolder(binding)
}
override fun bindData(
holder: BaseViewHolder<BannerData?>?,
data: BannerData?,
position: Int,
pageSize: Int
) {
if (holder is DataBindingViewHolder) {
holder.binding.bannerData = data
}
}
override fun getLayoutId(viewType: Int): Int {
return layout.item_slide_model_data_binding
}
}
internal class DataBindingViewHolder(var binding: ItemSlideModelDataBindingBinding) : BaseViewHolder<BannerData?>(binding.root)
item_slide_model_data_binding.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:binding="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="bannerData"
type="com.example.zhpan.banner.net.BannerData" />
</data>
<com.example.zhpan.banner.view.CornerImageView
android:id="@+id/banner_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/image_description"
android:scaleType="centerCrop"
binding:placeholder="@{bannerData.getPlaceHolder()}"
binding:url="@{bannerData.getImagePath()}" />
</layout>