正在发生的事情:即使如下图所示,val list: ArrayList<StudentModel>
集合中有两个元素,并且我在2
中明确指定了大小getItemCount()
。 onBindViewHolder
仅触发一次
AdapterCode
class ListAdapter(private val list: ArrayList<StudentModel>,val context: Context) : RecyclerView.Adapter<MovieViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
return MovieViewHolder(LayoutInflater.from(context).inflate(R.layout.frag_disp_group_blocks, parent, false))
}
override fun getItemCount(): Int {
Timber.i("$list.size")
return 2
}
override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val movie = list[position]
Timber.i("$movie")
Timber.i("$movie.get(position)")
holder?.tvAnimalType?.text = ""
}
}
class MovieViewHolder (view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val tvAnimalType = view.list_title
}
答案 0 :(得分:1)
return MovieViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.frag_disp_group_blocks, parent, false))
答案 1 :(得分:0)
这是我的错误....我已经给了matchParent参数,因此它触发了一次。 wrapContent确实解决了问题