The
RecyclerView class supports the display of a collection of data.
It is a modernized version of the
ListView and the GridView classes provided by the Android framework. Recycler view addresses several issues that the existing widgets have. It enforced a programming style that results in good performance. It also comes with default animations for removing and adding elements.RecyclerView allow to use different layout managers for positioning items.
Recycler view uses a
ViewHolder to store references to the views for one entry in the recycler view. A ViewHolder class is a static inner class in your adapter which holds references to the relevant views. With these references your code can avoid the time-consuming findViewById() method to update the widgets with new data.
Components of a RecyclerView
LayoutManagers
A
RecyclerView needs to have a layout manager and an adapter to be instantiated. A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user.
RecyclerView provides these built-in layout managers:
LinearLayoutManagershows items in a vertical or horizontal scrolling list.GridLayoutManagershows items in a grid.StaggeredGridLayoutManagershows items in a staggered grid.
To create a custom layout manager, extend the RecyclerView.LayoutManager class.
Here is Dave Smith's talk on the custom layout manager
Notes: In the recent version of the Support Library, if you don't explicitly set the LayoutManager, the RecyclerView will not show! There is a Logcat error thoughE/RecyclerView: No layout manager attached; skipping layout
RecyclerView.Adapter
RecyclerView includes a new kind of adapter. It’s a similar approach to the ones you already used, but with some peculiarities, such as a required ViewHolder. You will have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view. The good thing about this is that the first method is called only when we really need to create a new view. No need to check if it’s being recycled.
ItemAnimator
RecyclerView.ItemAnimator will animate ViewGroup modifications such as add/delete/select that are notified to the adapter. DefaultItemAnimator can be used for basic default animations and works quite well. See the section of this guide for more information.Using the RecyclerView
Using a
RecyclerView has the following key steps:- Add
RecyclerViewAndroidX library to the Gradle build file - Define a model class to use as the data source
- Add a
RecyclerViewto your activity to display the items - Create a custom row layout XML file to visualize the item
- Create a
RecyclerView.AdapterandViewHolderto render the item - Bind the adapter to the data source to populate the
RecyclerView
The steps are explained in more detail below.
Installation
Make sure the RecyclerView AndroidX library is listed as a dependency in your
app/build.gradle:dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
Click on "Sync Project with Gradle files" to let your IDE download the appropriate resources









%2B(1).png)
0 comments: