A1M:
私は交換しViewBindingsを使用することができfindViewById
、この典型的にRecyclerView.Adapter
初期化コード?私は設定できませんbinding
ViewHoldersは、セル当たり異なるなどのオブジェクトにvalのを。
class CardListAdapter(private val cards: LiveData<List<Card>>) : RecyclerView.Adapter<CardListAdapter.CardViewHolder>() {
class CardViewHolder(val cardView: View) : RecyclerView.ViewHolder(cardView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val binding = CardBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return CardViewHolder(binding.root)
}
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
val title = holder.cardView.findViewById<TextView>(R.id.title)
val description = holder.cardView.findViewById<TextView>(R.id.description)
val value = holder.cardView.findViewById<TextView>(R.id.value)
// ...
}
A1M:
私は2つのオプションが見つかりました:
使用bind
Bindingクラスのメソッドを
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
val binding = CardBinding.bind(holder.cardView)
// binding.title ...
または添付binding
ViewHolder代わりのビューに
class CardViewHolder(val binding: CardBinding) : RecyclerView.ViewHolder(binding.root)
その後にアクセスします。
holder.binding.title