ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

How to make a RecyclerView in Jetpack Compose — Part 1

Ian Alexander
ProAndroidDev
Published in
2 min readDec 7, 2020

Written as of alpha08. Compose is evolving rapidly so some syntax may have changed.

Edit — updated syntax with alpha09 changes

If you’ve developed for android you’ve inevitably come acrossRecyclerView, it’s a massive improvement over ListView which came before, but still pretty far from fun. Making a RecyclerView involves a messy pile of boilerplate — often difficult to re-use between screens — and complexity even for basic tasks, like headers, item callbacks, and multiple item types.

How does Jetpack Compose make lists simpler?

Creating a list in compose

Creating a list in compose is ridiculously easy. No adapters. No view holders.

All you need is a defined data structure and composables to build list items. That’s really it. No joke.

Item click callbacks

Adding events to list items is also really simple with the use of kotlin lambdas.

Multiple item types

Jetpack Compose is data driven, so to have multiple item types we start with building the data structure —something which becomes common in compose world.

And when creating the list use a when statement to choose which composable to make depending on the data item.

With compose we can spend less time wiring the UI together and more time building attractive UI components backed by solid data structures.

Abstracting logic

What you might have noticed about the code samples above is that the view is really dumb.

Compose makes our view layer dumb by default. In old android you had to work to take logic out of the view layer and into the presentation layer — Presenters, ViewModels, etc — in Jetpack Compose you’d have to work to put logic into the view layer. Good architecture becomes the path of least resistance in compose world.

In compose the logic is all in the data structure and how we manipulate the data structure to display in the UI — the view is made of small re-usable components which react to changes in the data structure. This not only lets us move logic into higher levels of our app’s architecture with ease, it actively encourages it.

For lists, it makes our job ridiculously simple. But how does this work in reality? In the next post I take a look

https://ian-alexander.medium.com/how-to-make-a-recyclerview-in-compose-part-2-ef9309324f46

Happy composing!

For full samples see the repo here

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Ian Alexander

Software developer experienced in running KMM teams across Octopus Energy

Responses (3)

Write a response

But there is much more than preparing the Views. This is actually relatively the easy part (view-binding and XML preparation).
There are many other common things that you do on RecyclerView: multi-selection, dragging, searching (filtering), modification of items (requires ID for each item), ...

--

"Compose makes our view layer dumb by default. In old android you had to work to take logic out of the view layer and into the presentation layer" - how so? I have never started a project that has logic in the view layer - ever. If it has logic, it was put there by the programmer.

--

How about animations? I use the adding/removing/reordering animations that recyclerview offers out of the box all the time. This solution only offers a simple list.

--