RecyclerView in Android

RecyclerView is a ViewGroup that helps us to display a scrollable list of elements. It is one of the most used UI components in the Android apps and it involves having a data source, an adapter, and the RecyclerView. Other than displaying the content, RecyclerView is efficiently reusing the views that have scrolled off the screen by recycling them.

📃Components Overview




👩🏻‍💻Implementation steps

To cover the implementation steps we will display a list of emails. The data source is hardcoded, right now we are focusing on learning how to use RecyclerView.

Step 1: Get the data by creating a new object class (data source)

Step 2: Add the RecyclerView dependency to app/build.gradle file and add RecyclerView to layout

Step 3: Create XML layout for item

Step 4: Define the LayoutManager in activity

Step 5: Extend RecyclerView.ViewHolder in a separated class

Step 6: Extend RecyclerView.Adapter in a separated class

Step 7: In onCreate of activity, create a RecyclerView with adapter and layout manager

🔔Notify the Adapter

To not impact the speed of rendering the UI elements for a RecyclerView make sure that you’re not calling notifyDataSetChanged(), setAdapter(Adapter), or swapAdapter(Adapter, boolean) for small updates. [official recommendation] The solution is to use SortedList or DiffUtil to create minimal updates when the data source has changed.

✨Item decorators

We could set dividers between the items using DividerItemDecoration

💫Swipe to refresh

Step 1: Add a new dependency in the gradle file

Step 2: Wrap the RecyclerView in a SwipeRefreshLayout

Step 3: Update the code in the Adapter

Step 4: Setup SwipeRefreshLayout

You could check the full source code here. (different branches for each section)

📚Learn more

Enjoy and feel free to leave a comment if something is not clear or if you have questions. And if you like it please share!

Thank you for reading! 🙂

Follow me on: Twitter | Medium |

Originally published at on December 28, 2020.

Squad Lead Developer @orangeromania ✨ Android @GoogleDevExpert ✨ Blogger, Speaker, Trainer #android #kotlin #gde #agile #scrum