ItemHolder¶
当你使用 BaseAdapter , BaseListAdapter , BasePagingAdapter 时,你需要继承 ItemHolder 并实现 HolderFactory 接口。以下为示例:
class DefaultImageHolder(itemView: View) : ItemHolder<Images.Image>(itemView) {
private val image: ShapeableImageView = itemView.findViewById(R.id.iidImage)
private val title: MaterialTextView = itemView.findViewById(R.id.iidTitle)
override fun onBindData(item: Images.Image) {
image.load(item.url) {
crossfade(true)
placeholder(R.drawable.background)
}
title.text = item.title
}
companion object : HolderFactory<Images.Image> {
override fun onCreateHolder(parent: ViewGroup, viewType: Int): DefaultImageHolder {
val view =
LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return DefaultImageHolder(view)
}
override val layoutId: Int
get() = R.layout.item_image_default
}
}
并将其作为参数传递给适配器,以下为示例:
private class ImageAdapter(context: Context) :
BaseAdapter<Images.Image>(context, mutableListOf(DefaultImageHolder.Companion)) {
... // 其他代码
}