For the More Curious: ListView and GridView

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 that of a RecyclerView. 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.

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, 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)
..................Content has been hidden....................

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