Create A List With RecyclerView - Android Developers
Create A List With RecyclerView - Android Developers
com/dev-summit)
If your app needs to display a scrolling list of elements based on large data sets (or data that
frequently changes), you should use RecyclerView
(/reference/androidx/recyclerview/widget/RecyclerView.html) as described on this page.
Tip: Start with some template code in Android Studio by clicking File > New > Fragment > Fragment (List).
Then simply add the fragment to your activity layout
(/training/basics/fragments/creating.html#AddInLayout).
If you'd like to create a list with cards, as shown in figure 2, also use the CardView
(/reference/androidx/cardview/widget/CardView.html) widget as described in Create a Card-based
Layout (/guide/topics/ui/layout/cardview.html).
If you'd like to see some sample code for RecyclerView, check out the RecyclerView Sample
App Java (https://github.com/android/views-widgets-samples/tree/master/RecyclerView) | Kotlin
(https://github.com/android/views-widgets-samples/tree/master/RecyclerViewKotlin).
RecyclerView overview
The views in the list are represented by view holder objects. These objects are instances of a
class you define by extending RecyclerView.ViewHolder
(/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder.html). Each view holder is in
charge of displaying a single item with a view. For example, if your list shows music
collection, each view holder might represent a single album. The RecyclerView
(/reference/androidx/recyclerview/widget/RecyclerView.html) creates only as many view holders as
are needed to display the on-screen portion of the dynamic content, plus a few extra. As the
user scrolls through the list, the RecyclerView
(/reference/androidx/recyclerview/widget/RecyclerView.html) takes the off-screen views and
rebinds them to the data which is scrolling onto the screen.
The view holder objects are managed by an adapter, which you create by extending
RecyclerView.Adapter (/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html). The
adapter creates view holders as needed. The adapter also binds the view holders to their
data. It does this by assigning the view holder to a position, and calling the adapter's
onBindViewHolder()
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html#onBindViewHolder(VH, int))
method. That method uses the view holder's position to determine what the contents should
be, based on its list position.
When the list is first populated, it creates and binds some view holders on either side of
the list. For example, if the view is displaying list positions 0 through 9, the
RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) creates and binds
those view holders, and might also create and bind the view holder for position 10. That
way, if the user scrolls the list, the next element is ready to display.
When the displayed items change, you can notify the adapter by calling an appropriate
RecyclerView.Adapter.notify…()
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html#notifyItemChanged(int))
method. The adapter's built-in code then rebinds just the affected items.
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
KOTLIN (#KOTLIN)JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
To feed all your data to the list, you must extend the RecyclerView.Adapter
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html) class. This object creates
views for items, and replaces the content of some of the views with new data items when the
original item is no longer visible.
The following code example shows a simple implementation for a data set that consists of an
array of strings displayed using TextView (/reference/android/widget/TextView.html) widgets:
KOTLIN (#KOTLIN)JAVA