Not so long ago, I started writing under android and always used ListView, after which I gave my code for review and I was told that using ListView "not Camille", unfortunately did not prove why. How are they so radically different and how is RecyclerView better than ListView?
3 answers
RecyclerView is a new view ViewGroup prepared for the interpretation in the same way of any view using adapters. It is assumed that he will be the heir of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layout. Use RecyclerView when you have data sets whose elements change at run time based on user actions or network events.
RecyclerView differs from its predecessor Listview primarily because of the following features:
1) the need to use the ViewHolder in the adapter - Listview adapters do not require the use of the ViewHolder pattern to improve performance. In contrast, the execution of the adapter for RecyclerView requires the use of the ViewHolder pattern;
2) Customizable element layers - A Listview can only arrange elements in a vertical linear order and this cannot be changed. Unlike Listview, RecyclerView has RecyclerView.Layoutmanager, which allows any placement of items, including horizontal lists or grids in a checkerboard pattern;
3) Light animation of elements - Listview does not contain any special mechanisms by which you can animate the addition or removal of elements. Unlike Listview, RecyclerView has RecyclerView.ItemAnimator, which allows you to control the animation;
4) Manually-installed data source — the Listview had adapters for various sources, such as an array adapter and a CursorAdapter for arrays and databases, respectively. In contrast, the RecyclerView.Adapter requires a custom implementation of data delivery to the adapter;
5) Manual decoration of elements - ListView has android: divider for easy separation of elements in the list. Unlike Listview, RecyclerView has RecyclerView.ItemDecoration, which provides more options for decorating a partition;
6) Definition of clicking on an item - ListView has an AdapterView.OnItemClickListener interface for associating click events with a specific item in the list. Unlike Listview, RecyclerView supports only RecyclerView.OnItemTouchListener which manages individual push events, but does not have built-in push controls.
Let's start with the fact that the RecyclerView widget is visually similar to the ListView and seems to do the same thing, but it is not a direct replacement for it and direct confrontation is somewhat incorrect here. Both of these widgets have their own "niches" in the structure of the Android application.
The need for the RecyclerView widget came about because of the inability of the ListView to display complex items with many different types of View and their relative positions in the list. The ListView had some technical errors, which made it not as fast and efficient as we would like.
As a result, the RecyclerView widget completely redesigned the algorithm for re-using items (using the Recycler subclass and some methods for interacting with the re-use process), the implementation of the ViewHolder subclass, which caches the links to the View items and optimizes the load on the device, was also mandatory. Also, several more helper classes for various optimizations have been implemented, which should improve performance. It is worth noting that all this is so confusing the source code of the widget that to understand its work is not an easy task.
In addition, visual design subclasses were implemented, representing a simpler way to “decorate” this widget - ItemDecoration , ItemAnimator , LayoutManager , etc., which contributes to the comfortable creation of really visually attractive lists without any crutches. Also, there were methods for working with individual items / group of items for inserting, replacing and deleting them from the list, against updating the entire list for ListView , which has a positive effect on responsiveness and performance, which also have action animation that removes these efforts from the developer .
However, it was not without "disappointments". The widget does not have any implemented adapter for working with sample data, any adapter is required to be implemented independently, based on the RecyclerView.Adapter blank class, listeners to clicks in items are also not implemented, they are also proposed to be implemented by themselves for some reason .
As a result, let's take the above mind and imagine that we need a simple primitive list on one text line without any “decorations”, like Spinner (for example, choosing a city from the list in the dialog box or other typical tasks of such a plan, where the spinner is not very appropriate) or side menu items NavigationView and similar tasks display lists. We don’t need to delete or insert anything into such a list, don’t need to decorate or animate it, and primitive content in a dozen of items does not require serious optimizations.
It is quite obvious that all the "bells and whistles" of RecyclerView in such an application are not only out of place, but also an extra load, but the necessary things, like a ready-made adapter for displaying simple lines, ready-made markup and a listener for pressing, are absolutely absent. As a result, instead of the three lines for ListView implementation of the simplest list will take to write a whole adapter class and markup files, which is a little upsetting.
Thus, the ways of applying these widgets are in different planes. RecyclerView for "magnificent" complex lists with many elements, animations and visual design, ListView for simple one-two-line lists (possibly with a picture) to standard data sets, like ArrayList, Cursor and so on. where everything you need is already implemented by the system itself.
The choice between them may lie in the plane that if the ListView requires an adapter redefinition (creating a custom adapter), then you should already think about RecyclerView . If there is no such need, then the RecyclerView capabilities will most likely be out of place.
- RecyclerView was created as an updated and updated version of the widget for lists.
- Google recommends switching to RecyclerView for reasons of claim 1.
- In RecyclerView set of any add. pieces For example, the ability to specify the adapter in more detail how to display changes in the data. For a particular item, for a group of items. ListView could only change all items at once.
- RecyclerView cannot be used without Viewholder, which offers several advantages.
- Animations for add / delete / modify data events are built into RecyclerView immediately.
- etc