将 myAdapter 合成转换为视图绑定

convert myAdapter synthetic to viewbinding

我很困惑,如何将我的适配器视图从合成更新为视图绑定! 这是我的代码,如何定义视图绑定到 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

    }

}

就像在片段中定义视图绑定一样简单 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

    }}

我认为这最适合你

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"
        }
    }
}