RecyclerView came into existence during the launch of Android version 5.0 (Lollipop). It came as a powerful and flexible enhancement over ListView. When we look at the implementation side of the RecyclerView, we find that it makes an optimised use of item layouts.
In ListView, all items are inflated in one go. While in RecyclerView, the inflated item layouts are reused.
|RecyclerView vs ListView in android|
This feature of RecyclerView enables to use minimum RAM, easy scrolling and drastically improves performance. As layouts are inflated dynamically, developers can now add item animators. Item animators means new items are added to the list with animation. Listing below are few advantages of RecyclerView over ListView.
ViewHolder Pattern: RecyclerView makes use of ViewHolder class. This class is used to define the views available for each layout. For example: An item layout contains an ImageView and a TextView. Then the corresponding objects are defined here using their ids. Below is the code snippet of a RecyclerView Adapter that will clearly show how ViewHolder class is used:
This was also available in ListView but its use was never made a compulsion.
LayoutManager: A massive enhancement to RecyclerView, decides how the items are to be displayed. ListView could only implement vertical display of items. RecyclerView supports the following type of layouts for its items:
LinearLayoutManager - which supports both vertical and horizontal lists,
GridLayoutManager - which supports displaying grids as seen in Gallery apps.
StaggeredLayoutManager - which supports item layouts of variable sizes like staggered lists. It is much like grid layout with items of variable heights.
Item Animator: ListView do not provide item animations. In RecyclerView you can either use the default animation available or define your own using the RecyclerView.ItemAnimator class. Proper animations have always improved user interface.
OnItemTouchListener: RecyclerView’s OnItemTouchListener is equivalent to ListView’s AdapterView.OnItemClickListener but with much more power. It is used to interpret various touch events like MotionEvent.ACTION_UP, MotionEvent.ACTION_DOWN, etc. and carry out the further feature implementations.
Though there are so many advantages, its implementation is clumsy and lengthy. A beginner requires more time is required to understand RecyclerView than ListView. So we see that the advantages come with the cost of difficulty of writing multiple code lines.
There are places where ListView can be preferred over RecyclerView. ListView can still be used where there are less number of items in the list, where a simple static list needs to be displayed. But if you need the app to be scalable, you need to implement RecyclerView. Moreover, it will never harm you if you prefer to use RecyclerView everytime.