i'm confused, how can i update my adapter views from synthetic to viewbinding! here is my code, how define viewbinding to viewHolder?
class PlayersListAdapter(
var items: ArrayList<MatchPlayer>
) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflate: View =
LayoutInflater.from(parent.context)
.inflate(R.layout.item_team_player, parent, false)
return MatchSubstitutionHolder(inflate)
}
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
viewHolder.itemView.lblPlayerName.text = "name"
}
class MatchSubstitutionHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var view = itemView
}
override fun getItemCount(): Int {
return items.size
}
}
CodePudding user response:
it's so easy like define viewbinding in fragment or activity
class PlayersListAdapter(
var items: ArrayList<MatchPlayer>
) :
RecyclerView.Adapter<TeamPlayersListAdapter.MatchSubstitutionHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MatchSubstitutionHolder {
val binding= ItemTeamPlayerBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return MatchSubstitutionHolder(binding)
}
override fun onBindViewHolder(viewHolder: MatchSubstitutionHolder, position: Int) {
viewHolder.binding.lblPlayerName.text = "name"
}
class MatchSubstitutionHolder(val binding: ItemTeamPlayerBinding) : RecyclerView.ViewHolder(binding.root)
override fun getItemCount(): Int {
return items.size
}}
CodePudding user response:
I think this is best for you
class PlayersListAdapter(var items: ArrayList<MatchPlayer>) :
RecyclerView.Adapter<TeamPlayersListAdapter.MatchSubstitutionHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MatchSubstitutionHolder {
val binding = ItemTeamPlayerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return MatchSubstitutionHolder(binding)
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(viewHolder: MatchSubstitutionHolder, position: Int) {
viewHolder.bind(position)
}
class MatchSubstitutionHolder(val binding: ItemTeamPlayerBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(position: Int) {
val matchPlayer = items[position]
binding.lblPlayerName.text = "name"
}
}
}