0% found this document useful (0 votes)
256 views5 pages

Create A List With RecyclerView - Android Developers

This document provides information and code samples for implementing a scrolling list with RecyclerView in Android. It describes adding RecyclerView to layouts, connecting it to a layout manager and adapter, and creating a basic list adapter to display data from an array. RecyclerView is recommended over ListView for lists with large or changing datasets as it recycles views to improve performance.

Uploaded by

Belllaa Edward
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
256 views5 pages

Create A List With RecyclerView - Android Developers

This document provides information and code samples for implementing a scrolling list with RecyclerView in Android. It describes adding RecyclerView to layouts, connecting it to a layout manager and adapter, and creating a basic list adapter to display data from an array. RecyclerView is recommended over ListView for lists with large or changing datasets as it recycles views to improve performance.

Uploaded by

Belllaa Edward
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

up for livestream updates. (https://developer.android.

com/dev-summit)

Create a List with RecyclerView   


Part of Android Jetpack (/jetpack).

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).

Figure 1. A list using RecyclerView Figure 2. A list also using CardView

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 RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) widget is a more


advanced and flexible version of ListView (/reference/android/widget/ListView.html).

In the RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) model, several


different components work together to display your data. The overall container for your user
interface is a RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) object
that you add to your layout. The RecyclerView
 (/reference/androidx/recyclerview/widget/RecyclerView.html) fills itself with views provided by a
layout manager that you provide. You can use one of our standard layout managers (such as
LinearLayoutManager (/reference/androidx/recyclerview/widget/LinearLayoutManager.html) or
GridLayoutManager (/reference/androidx/gridlayout/widget/GridLayoutManager.html)), or implement
your own.

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.

This RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) model does a lot of


optimization work so you don't have to:

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.

As the user scrolls the list, the RecyclerView


 (/reference/androidx/recyclerview/widget/RecyclerView.html) creates new view holders as
necessary. It also saves the view holders which have scrolled off-screen, so they can be
reused. If the user switches the direction they were scrolling, the view holders which
were scrolled off the screen can be brought right back. On the other hand, if the user
keeps scrolling in the same direction, the view holders which have been off-screen the
longest can be re-bound to new data. The view holder does not need to be created or
have its view inflated; instead, the app just updates the view's contents to match the
new item it was bound to.

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.

Add the support library

To access the RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) widget,


you need to add the v7 Support Libraries (/tools/support-library/features.html#v7) to your project
as follows:

1. Open the build.gradle file for your app module.

2. Add the support library to the dependencies section.

dependencies {
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}

Add RecyclerView to your layout

Now you can add the RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html) to


your layout file. For example, the following layout uses RecyclerView
 (/reference/androidx/recyclerview/widget/RecyclerView.html) as the only view for the whole layout:

<?xml version="1.0" encoding="utf-8"?>


<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Once you have added a RecyclerView (/reference/androidx/recyclerview/widget/RecyclerView.html)


widget to your layout, obtain a handle to the object, connect it to a layout manager, and attach
an adapter for the data to be displayed:

KOTLIN (#KOTLIN)JAVA

public class MyActivity extends Activity {


    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes


        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);
        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // specify an adapter (see also next example)


        mAdapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(mAdapter);
    }
    // ...
}

Add a list adapter

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


    private String[] mDataset;

    // Provide a reference to the views for each data item


    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)


    public MyAdapter(String[] myDataset) {

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy