Devrath
Devrath

Reputation: 42854

OnBindViewHolder is triggered only once

What is Happening: Even though there are two elements in val list: ArrayList<StudentModel> collection as seen in image below and I have explicitly given size 2 in getItemCount(). Only once onBindViewHolder is triggered

enter image description here

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
}

Upvotes: 0

Views: 565

Answers (2)

Devrath
Devrath

Reputation: 42854

This was a mistake from my side .... I had given the matchParent params so it was triggering once. wrapContent did solve the problem

Upvotes: 1

Jack
Jack

Reputation: 142

passing a context from activity/fragment is not necessary unless they serve the purpose of their usage. Use context from the onCreateViewHolder parameter parent.

return MovieViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.frag_disp_group_blocks, parent, false))

Upvotes: 1

Related Questions