1

I am working with GridLayoutManager and I have encountered unexpected RecyclerView's behaviour. If spanCount is greater than 4, the RecyclerView continuously recreates ViewHolders on scrolling.

    override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.main_activity)

       val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
       recyclerView.layoutManager = GridLayoutManager(this, 7)
       recyclerView.adapter = Adapter()
   }

   private class Adapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
       //continuously invokes while scrolling:
       override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
           val view:View = TextView(parent.context).apply {
               text = "Hello!"
           }
           return object : RecyclerView.ViewHolder(view) {}
       }

       override fun getItemCount(): Int = 3500

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

How to fix it and force RecyclerView.Adapter to reuse ViewHolders?

  • I think you should not create object of Text inside onCreateViewHolder. You a ViewHolder class – BraveEvidence Oct 26 '18 at 10:40
  • @Vitali there seems like missing code in the `onBindViewHolder` method. Can you please complete the code so that we can understand what is happening here? Currently, since it is empty ideally you won't see anything that should be displayed. – Mohit Ajwani Oct 26 '18 at 10:45
  • Check this tutorial for better understanding. https://android.jlelse.eu/using-recyclerview-in-android-kotlin-722991e86bf3 – Mohit Ajwani Oct 26 '18 at 10:46

1 Answers1

0

I reproduced your issue with this code (copy-pasteable, no resource files required):

class RecActivity : AppCompatActivity() {
    lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        recyclerView = RecyclerView(this)
        setContentView(recyclerView)
        recyclerView.layoutManager = GridLayoutManager(this, 7)
        recyclerView.adapter = Adapter()
    }

    inner class Adapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        var vhCount = 0
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val txtView = AppCompatTextView(this@RecActivity)
            txtView.tag = vhCount++.toString()
            txtView.gravity = Gravity.CENTER
            title = vhCount.toString()  // display # of created VHs in title
            return object : RecyclerView.ViewHolder(txtView){}
        }

        override fun getItemCount() = 3500

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder.itemView as TextView).text = "$position (${holder.itemView.tag})"
        }
    }
}

The problem is there's not enough views to fill out entire rows in the RecycledViewPool. By default there's only 5 items per ViewType, so having wide rows of 7 force creation of more ViewHolders while scrolling. To fix this issue, simply increase size of your RecycledViewPool like so (in onCreate):

recyclerView.layoutManager = GridLayoutManager(this, 7)
recyclerView.adapter = Adapter()
// add line below: 0 is default itemViewType, 14 is two rows of items which should be enough
recyclerView.recycledViewPool.setMaxRecycledViews(0, 14)
Pawel
  • 15,548
  • 3
  • 36
  • 36