RecyclerView: Basics to advanced

Guneet Singh
3 min readApr 24, 2020
Source: https://gph.is/2p9HBUm

RecyclerView is one of the most used components in the android system and if you have any experience with android you must have used it even more than once. But do you really use it the right way? Haven’t you ever got an unexpected result while using RecyclerView in your project? Or have you explored the capabilities of RecyclerView? Well, I did and that’s the reason why I decided to share this. The answer lies in understanding how the RecyclerView works first!

In this article, I will talk about the basics of RecyclerView. In the end, I will leave some references to follow.

What is RecyclerView?

The answer is a lot in the name. It recycles child views in the list and reuses them instead of instantiating a new one every time it’s required and in that way, it helps to save a lot of memory when a huge list is loaded, simply by reusing scrolled/offscreen/hidden views. You can set this limit as well:-

val recyclerView= view.findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.setItemViewCacheSize(5)

Although you don’t need to mess with it as android itself manages this limit quite well. Optionally, to optimize you can tell RecyclerView whether it has a fixed size. The caveat is you should only do it when it’s true:-

recyclerView.setHasFixedSize(true)

How does recycling work programmatically?

Basically, Every time a new item in the list needs to load up, at the place calling onCreateViewHolder it simply calls onBindViewHolder if it has appropriate views in the pool to reuse!

Tips

Ever got -1 value on calling getAdapterPosition? This is the RecyclerView.NO_POSITIONconstant and is returned when the ViewHolder is sent to RecycledViewPool or it's not bound to the Adapter yet (mostly!). And thats why you shouldn’t rely on getAdapterPosition in the constructor of your ViewHolder. Remember this when you use it next time!

The most reliable position of the view is the one you get inonBindViewHolder. Use this position whenerver possible!

More

Ever tried to create RecyclerView with multiple view types? If not then you might also never been curious about the viewType parameter in the call:-

public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);

That’s actually possible and you can use different ViewHolders for different positions. All you need to take care of is to override getItemViewType:-

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class ViewHolder0 extends RecyclerView.ViewHolder {
...
public ViewHolder0(View itemView){
...
}
}
class ViewHolder2 extends RecyclerView.ViewHolder {
...
public ViewHolder2(View itemView){
...
}
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder0(...);
case 2: return new ViewHolder2(...);
...
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case 0:
ViewHolder0 viewHolder0 = (ViewHolder0)holder;
...
break;
case 2:
ViewHolder2 viewHolder2 = (ViewHolder2)holder;
...
break;
}
}
}

Advanced

There is still a lot you can do with RecyclerView. Much that can’t be covered in one article. And now with the help of the JetPack architecture library, you can even implement paging in your project on top of the same old RecyclerView like a breeze. Exploration never ends but I will end up sharing two good examples I have explored when I was new to Android:-

More

I have written one more article on RecyclerView that you must read:

--

--