Add items Dynamically in RecyclerView with Example | For Begineers

Utsav Verma / / 0

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

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:
  • LinearLayoutManager shows items in a vertical or horizontal scrolling list.
  • GridLayoutManager shows items in a grid.
  • StaggeredGridLayoutManager shows 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 though E/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:
  1. Add RecyclerView AndroidX library to the Gradle build file
  2. Define a model class to use as the data source
  3. Add a RecyclerView to your activity to display the items
  4. Create a custom row layout XML file to visualize the item
  5. Create a RecyclerView.Adapter and ViewHolder to render the item
  6. 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












Utsav Verma


Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut odio. Nam sed est. Nam a risus et est iaculis adipiscing. Vestibulum ante ipsum faucibus luctus et ultrices.
Follow me @Bloggertheme9

0 comments:

About Me

My photo
I work as an Analyst in HCL Technologies. Android App Development is my hobby. As a passionate technology lover, you need to keep yourself updated with trending technologies. The best way to learn and stay up to date is simply to do something creative. I’ve a number of small personal app projects and each one have been rewarding and useful in one way or the other.