RecyclerView is better than ListView
Monday, March 2, 2015ListView had problems. RecyclerView fixed them.
ViewHolder is mandatory now
Remember how ViewHolder was optional in ListView and everyone forgot to use it? Now you cant forget. The adapter requires it:
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
// create holder
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
// bind data to holder
}
}
Good. No more slow lists because someone didnt know the pattern.
LayoutManagers
This is the best part. Want a grid? Use GridLayoutManager. Horizontal list? LinearLayoutManager with horizontal orientation. Staggered grid? StaggeredGridLayoutManager.
All the same adapter. Just change the LayoutManager.
recyclerView.layoutManager = GridLayoutManager(context, 2)
// now its a 2 column grid
With ListView you needed different widgets for different layouts. Now its all RecyclerView.
ItemDecoration and ItemAnimator
You can add dividers, spacing, whatever without touching the adapter. And animations when items change are handled for you.
Migration
We migrated from ListView to RecyclerView in 2015. Little more code for simple lists, but way more flexible.
Worth it.