Simplifying RecyclerView using Groupie.

Soosy Amoora
2 min readMay 24, 2020

--

RecyclerViewAdapter can be very complex especially when it comes to dealing with different types of items. Groupie simplifies that for us.

Groupie is a simple, flexible library for complex RecyclerView layouts. It makes it easy to handle asynchronous content updates and insertions and user-driven content changes. At the item level, It abstracts the boilerplate of item view types, item layouts, viewholders, and span sizes.

Here is a simple example while using Groupie.

Let’s get started!

Add dependencies in Gradle Build(Module: app)

implementation "com.xwray:groupie:$groupie_version"
implementation "com.xwray:groupie-kotlin-android-extensions:$groupie_version"
implementation ("io.coil-kt:coil:0.11.0")

We will be adding the Groupie dependencies only( you can check more)for this post we would only be needing these, and we will be using coil, an image loading library for Android.

Create Layouts

We created the activity_main.xml which contains our RecyclerView (set the layout manager attribute to LinearLayout) and the people_layout.xml which is the layout for the RecyclerView items.

RecyclerView Adapter

GroupieAdapter has taken care of this for us, Our only concern now is populate the adapter using adapter.add() That’s all we need; you can now add your new item directly to a GroupAdapter and then attach it to our RecyclerView.

val adapter = GroupAdapter<GroupieViewholder>()

Creating the Item Class

Groupie abstracts away the complexity of multiple item view types. Each Item declares a view layout id, and gets a callback to bind the inflated layout.

The PeopleItem class gives you simple callbacks to bind your model object to the generated fields. Because of Kotlin Android extensions, there’s no need to write a ViewHolder Class. Now we will override two methods bind() and getLayout(). The getLayout() will return the layout i.e people_layout.xml and the bind() method will bind our views to the data.

And thats it!

Groupie helps with more complex implementation of RecyclerView like treating your content as logical groups and handles change notifications for you, Think of sections with headers and footers, expandable groups, blocks of vertical columns, and much more.

Hope this helps, Thank you.

You can follow me on twitter @iamsoosy

--

--

Soosy Amoora

My Name is Saadat Aliyu|ex dsc lead| Love soul search |Kotlin User Group Kano lead| DevC from Facebook Lead|Github Campus Expert | Android Dev| Random Writer