What is the difference between ListView
and RecyclerView
on Android?
From what APi RecyclerView
is available?
Is it valid to use RecyclerView
and not ListView
?
What is the difference between ListView
and RecyclerView
on Android?
From what APi RecyclerView
is available?
Is it valid to use RecyclerView
and not ListView
?
The RecyclerView is a new (but not so much) view that came to replace the ListView and the GridView.
According to its documentation , it is a more advanced and efficient widget, when compared to its predecessors, and that it presents several simplifications to support animations and different dispositions of elements.
To offer all these optimizations, Google decided to simplify the element. It may seem strange, but RecyclerView has a lower level of accountability when compared to ListView. In theory, the widget is just a container that encapsulates a LayoutManager and an ItemAnimator, and that communicates with an Adapter, more precisely, a RecyclerView.Adapter.
What is the difference between ListView and RecyclerView on Android?
There are two differences between ListView and RecyclerView :
The RecyclerView is agnostic as to where the views are placed, how they are moved and how that movement is animated. This is accomplished by moving these responsibilities to a LayoutManager and a ItemAnimator , allowing the same adapter to visually represent the data in different ways:
Thisdifferenceisevidencedbyhoweachisinitialized:
ListView:
listView.setAdapter(myAdapter);
RecyclerView:
recyclerView.setAdapter(myRecyclerAdapter);recyclerView.setLayoutManager(newLinearLayoutManager(this));recyclerView.setItemAnimator(newDefaultItemAnimator());
Usesanadaptertype( Recycler.Adapter ) that implements the ViewHolder pattern.
A ViewHolder object is used to store each of the views of the layout , so they can be immediately accessed without the need to repeatedly use findViewById()
.
In the ListView this implementation was optional.
From which APi is RecyclerView available?
RecyclerView has appeared with Android 5 but is available for earlier versions through v7 recyclerview library .
Is it valid to use the RecyclerView and not the ListView?
The use of either is valid.
The RecyclerView is not properly a substitute for ListView . It is a new, more flexible, approach to providing a limited view of a large dataset.
Use RecyclerView if you want to take advantage of what it offers differently: custom layout and animations, if not, as long as you implement the ViewHolder pattern, use a ListView.