正常使用:
class SearchAdapter(val context: Context) : RecyclerView.Adapter<SearchVH>() {
var titleData: String? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchVH {
return SearchVH(
LayoutInflater.from(context).inflate(R.layout.search_adapter, parent, false)
)
}
override fun getItemCount() = if (titleData == null) 0 else 1
override fun onBindViewHolder(holder: SearchVH, position: Int) {
holder.title.text = "hello"
holder.tvLoadAll.text = "world"
}
}
class SearchVH(var view: View) : RecyclerView.ViewHolder(view) {
val title: TextView = view.findViewById(R.id.title)
val tvLoadAll: TextView = view.findViewById(R.id.tv_load_all)
}
使用viewbinding之后:
class SearchAdapter(val context: Context) : RecyclerView.Adapter<SearchVH>() {
var titleData: String? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchVH {
return SearchVH(
SearchAdapterBinding.inflate(LayoutInflater.from(context), parent, false)
)
}
override fun getItemCount() = if (titleData == null) 0 else 1
override fun onBindViewHolder(holder: SearchVH, position: Int) {
holder.binding.title.text = "hello"
holder.binding.tvLoadAll.text = "world"
}
}
class SearchVH(val binding: SearchAdapterBinding) :
RecyclerView.ViewHolder(binding.root)