Android RecyclerView onBindViewHolder调用两次(第一次创建适配器时)

时间:2019-04-17 17:33:28

标签: android kotlin android-recyclerview recycler-adapter

我有简单的RecyclerViewAdapter类:

class TariffsCardAdapter(context: Context, tariffCardItems: List<ItemsItem?>?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

   private var context: Context? = null
   private var tariffCardItems: ArrayList<ItemsItem?>? = null

    init {
      this.context=context
      this.tariffCardItems=tariffCardItems as ArrayList<ItemsItem?>
    }

   override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
       val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_tariff_card_items, parent, false)
       return TariffsCardAdapterViewHolder(view)
   }

   override fun getItemCount(): Int {
       return tariffCardItems!!.size
   }//getItemCount ends

   override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

     val viewHolder = holder as TariffsCardAdapterViewHolder
     Log.e("posX","pos:::".plus(position))
   }

   private inner class TariffsCardAdapterViewHolder : RecyclerView.ViewHolder {
      var text: TextView? = null
      constructor(row: View) : super(row) {
         text= row.findViewById(R.id.text) as TextView
      }
   }
}

现在假设我的 tariffCardItems:列表在列表中只有5个要显示的项目!

问题在于,适配器首次实例化时; onBindViewHolder方法调用了两次!

以便日志值显示为:

pos ::: 0

pos ::: 1

pos ::: 0

pos ::: 1

这意味着在此之后,我的回收者视图位置将为 1 ! 并且第一个视图将使用列表项位于1索引创建,这是错误的,应该为0

简单来说,回收站视图的第一个子级是由1个错误的位置值创建的。

有人可以找出我做错了什么吗

预先感谢和问候

1 个答案:

答案 0 :(得分:0)

这仅仅是因为您在代码中调用适配器2,recyclerview的bindviewholder应该用来绑定数据而不是调用viewholder。

class TariffsCardAdapter(context: Context, tariffCardItems: List<ItemsItem?>?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

   private var context: Context? = null
   private var tariffCardItems: ArrayList<ItemsItem?>? = null

    init {
      this.context=context
      this.tariffCardItems=tariffCardItems as ArrayList<ItemsItem?>
    }

   override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
       val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_tariff_card_items, parent, false)
       return TariffsCardAdapterViewHolder(view)
   }

   override fun getItemCount(): Int {
       return tariffCardItems!!.size
   }//getItemCount ends

   override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

                //bind data
   }

   private inner class TariffsCardAdapterViewHolder : RecyclerView.ViewHolder {
      var text: TextView? = null
      constructor(row: View) : super(row) {
         text= row.findViewById(R.id.text) as TextView
      }
   }
}

例如,您的适配器应该是这样的

class CustomAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    //this method is returning the view for each item in the list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
        return ViewHolder(v)
    }

    //this method is binding the data on the list
    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
        holder.bindItems(userList[position])
    }

    //this method is giving the size of the list
    override fun getItemCount(): Int {
        return userList.size
    }

    //the class is hodling the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(user: User) {
            val textViewName = itemView.findViewById(R.id.textViewUsername) as TextView
            val textViewAddress  = itemView.findViewById(R.id.textViewAddress) as TextView
            textViewName.text = user.name
            textViewAddress.text = user.address
        }
    }
}

此代码中的方法调用了两次

 val viewHolder = holder as TariffsCardAdapterViewHolder
     Log.e("posX","pos:::".plus(position))