Lists and Grids: Past, Present, and Future

The core Android OS includes ListView, GridView, and Adapter classes. Until the release of Android 5.0, these were the preferred ways to create lists or grids of items.

The API for these components is very similar to RecyclerView’s. The ListView or GridView class is responsible for scrolling a collection of items, but it does not know much about each of those items. The Adapter is responsible for creating each of the Views in the list. However, ListView and GridView do not enforce that you use the ViewHolder pattern (though you can – and should – use it).

These old implementations are replaced by the RecyclerView implementation because of the complexity required to alter the behavior of a ListView or GridView.

Creating a horizontally scrolling ListView, for example, is not included in the ListView API and requires a lot of work. Creating custom layout and scrolling behavior with a RecyclerView is still a lot of work, but RecyclerView was built to be extended, so it is not quite so bad. And, as you will see in Chapter 20, RecyclerView can be used with a GridLayoutManager to arrange items in a grid.

Another key feature of RecyclerView is the animation of items in the list. Animating the addition or removal of items in a ListView or GridView is a complex and error-prone task. RecyclerView makes this much easier; it includes a few built-in animations and allows for easy customization of these animations.

For example, if you found out that the crime at position 0 moved to position 5, you could animate that change like so:

    recyclerView.adapter.notifyItemMoved(0, 5)

RecyclerView is powerful and extensible, but it is also complex and requires a lot of setup for even simple UIs. With Jetpack Compose, which you will start learning about in Chapter 26, you have access to the LazyColumn and LazyRow composables. These composables have all the customizability and performance of RecyclerView, but they can be created with a fraction of the code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.140.68