Professional Documents
Culture Documents
Dynamic - 12456
Dynamic - 12456
RecyclerView makes it easy to efficiently display large sets of data. You supply
the data and
define how each item looks, and the RecyclerView library
dynamically creates the elements
when they're needed.
Note: Besides being the name of the class, RecyclerView is also the name of
the library. In
this page, RecyclerView in code font always means the class
in the RecyclerView library.
Key classes
Several different classes work together to build your dynamic list.
RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView)
is the ViewGroup
(/reference/android/view/ViewGroup) that contains the views
corresponding to your data.
It's a view itself, so you add RecyclerView
into your layout the way you would add
any other UI element.
Each individual element in the list is defined by a view holder object. When
the view
holder is created, it doesn't have any data associated with it. After
the view holder is
created, the RecyclerView binds it to its data. You
define the view holder by
extending
RecyclerView.ViewHolder
(/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder).
The RecyclerView requests those views, and binds the views to their data,
by calling
methods in the adapter. You define the adapter by extending
RecyclerView.Adapter
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter).
The layout manager arranges the individual elements in your list. You can
use one of
the layout managers provided by the RecyclerView library, or you can
define your own.
Layout managers are all based on the library's
LayoutManager
(/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager)
abstract class.
You can see how all the pieces fit together in the RecyclerView sample app
(Kotlin)
(https://github.com/android/views-widgets-samples/tree/main/RecyclerViewKotlin/) or
RecyclerView sample app
(Java)
(https://github.com/android/views-widgets-samples/tree/main/RecyclerView/).
First of all, decide what the list or grid is going to look like. Ordinarily
you'll be able to
use one of the RecyclerView library's standard layout managers.
Design how each element in the list is going to look and behave. Based
on this design,
extend the ViewHolder class. Your version of ViewHolder
provides all the
functionality for your list items. Your view holder is a
wrapper around a View , and that
view is managed by RecyclerView .
Define the Adapter that associates your data with the ViewHolder views.
LinearLayoutManager (/reference/androidx/recyclerview/widget/LinearLayoutManager)
arranges the items in a one-dimensional list.
GridLayoutManager (/reference/androidx/recyclerview/widget/GridLayoutManager)
arranges all items in a two-dimensional grid:
StaggeredGridLayoutManager
(/reference/androidx/recyclerview/widget/StaggeredGridLayoutManager)
is similar to
GridLayoutManager , but it does not require that items in a
row have the same height
(for vertical grids) or items in the same column
have the same width (for horizontal
grids). The result is that the items
in a row or column can end up offset from each
other.
You'll also need to design the layout of the individual items. You'll need this
layout when you
design the view holder, as described in the next section.
When you define your adapter, you need to override three key methods:
onCreateViewHolder()
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onCreateViewHolder(android.vi
ew.ViewGroup,%20int))
:
RecyclerView calls this method whenever it
needs to create a new ViewHolder .
The method creates and initializes the
ViewHolder and its associated View , but does
not fill in the view's
contents—the ViewHolder has not yet been bound to specific
data.
onBindViewHolder()
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,%20int))
:
RecyclerView calls this method to associate a ViewHolder with data. The
method
fetches the appropriate data and uses the data to fill in the view
holder's layout. For
example, if the RecyclerView displays a list of names,
the method might find the
appropriate name in the list and fill in the view
holder's TextView
(/reference/android/widget/TextView) widget.
getItemCount()
(/reference/androidx/recyclerview/widget/RecyclerView.Adapter#getItemCount()):
RecyclerView calls this method to get the size of the data set. For example, in
an
address book app, this might be the total number of addresses. RecyclerView
uses
this to determine when there are no more items that can be displayed.
KotlinJava (#java)
(#kotlin)
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
init {
// Define click listener for the ViewHolder's View.
textView = view.findViewById(R.id.textView)
}
}
return ViewHolder(view)
}
The layout for the each view item is defined in an XML layout file, as usual.
In this case, the
app has a text_row_item.xml file like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:layout_marginLeft="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:gravity="center_vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/element_text"/>
</FrameLayout>
Next steps
A simple RecyclerView implementation like this may be all you need. However,
the library
also offers many ways to customize your implementation. For more
information, see
Advanced RecyclerView
customization (/guide/topics/ui/layout/recyclerview-custom).
Additional resources
For more information about testing on Android, consult the following resources.
Sample apps
Content and code samples on this page are subject to the licenses described in the Content License (/license).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.