使用绑定会破坏 RecyclerView 项目布局

gro*_*roo 1 android kotlin android-recyclerview android-binder

我正在使用 Kotlin 构建 Android 应用程序,并决定替换对 findViewById 的调用并使用绑定。一切正常,但具体来说,当我更改 RecyclerView 的适配器时,它会破坏项目布局。

带有 findViewById 的原始代码:

class WeightListAdapter(val weights: List<WeightWithPictures>, val onWeightItemClickListener: OnWeightItemClickListener) : RecyclerView.Adapter<WeightListAdapter.WeightHolder>()  {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeightListAdapter.WeightHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item_weight, parent, false)
        return WeightHolder(view)
    }

    override fun onBindViewHolder(holder: WeightListAdapter.WeightHolder, position: Int) {
        val weightWithPictures = weights[position]
        holder.bind(weightWithPictures)
    }

    override fun getItemCount() = weights.size

    inner class WeightHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        private lateinit var weight: Weight
        private val weightValueView: TextView = this.itemView.findViewById(R.id.weightValue)
        private val weightDateView: TextView = this.itemView.findViewById(R.id.weightDate)
        private val weightImageView: ImageView = this.itemView.findViewById(R.id.weightImage) as ImageView
Run Code Online (Sandbox Code Playgroud)

这是布局:

在此输入图像描述

但每当我使用绑定时:

class WeightListAdapter(val weights: List<WeightWithPictures>, val onWeightItemClickListener: OnWeightItemClickListener) : RecyclerView.Adapter<WeightListAdapter.WeightHolder>()  {

    private var _binding: ListItemWeightBinding? = null
    private val binding get() = _binding!!

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeightListAdapter.WeightHolder {
        _binding = ListItemWeightBinding.inflate(LayoutInflater.from(parent.context))
        val view = binding.root
        return WeightHolder(view)
    }

    override fun onBindViewHolder(holder: WeightListAdapter.WeightHolder, position: Int) {
        val weightWithPictures = weights[position]
        holder.bind(weightWithPictures)
    }

    override fun getItemCount() = weights.size

    inner class WeightHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        private lateinit var weight: Weight
        private val weightValueView: TextView = binding.weightValue
        private val weightDateView: TextView = binding.weightDate
        private val weightImageView: ImageView = binding.weightImage
Run Code Online (Sandbox Code Playgroud)

布局中断:

在此输入图像描述

关于我在这里做错了什么有什么想法吗?这是一个错误吗?

PS - 现在,我只是添加注释以忽略项目视图中记录的绑定,但我真的很想了解出了什么问题。

Ten*_*r04 5

您的绑定需要在其父视图的上下文中展开,以便其根视图的布局参数将生效:

binding = ListItemWeightBinding.inflate(LayoutInflater.from(parent.context), parent, false)
Run Code Online (Sandbox Code Playgroud)

binding我认为,如果您尝试长期使用适配器,那么为适配器创建属性也会给您带来问题。每个 ViewHolder 都拥有一个具有不同绑定实例的不同视图。它现在可以工作,因为您仅在设置每个实例后立即将其用于 ViewHolder 实例化。但如果这就是您的全部意图,您应该将绑定传递给 ViewHolder 的构造函数并省略适配器的属性。

顺便说一下,这就是我用于 ViewHolders 的模式。更少的代码。请注意,它不一定是一个inner类。

class WeightHolder(binding: ListItemWeightBinding) : RecyclerView.ViewHolder(binding.root), View.OnClickListener {

    fun bind(item: WeightWithPictures) {
        with (binding) {
            // set data for views here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)