android-studio
  1. android-studio-recyclerview-list

Android Studio RecyclerView List

Syntax

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
recyclerview = findViewById(R.id.recyclerview);
recyclerview.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
adapter = new MyAdapter(myDataset);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(adapter);

Example

Suppose you want to display a list of items in your Android app using RecyclerView. You can create a layout file called item_layout.xml to define the UI for each item in the list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item Name"/>

</LinearLayout>

Then, in your activity or fragment, you can create a list of items using a List<String> and pass it to the MyAdapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> myDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView;

        public ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.imageView);
            textView = view.findViewById(R.id.textView);
        }
    }

    public MyAdapter(List<String> myDataset) {
        this.myDataset = myDataset;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(myDataset.get(position));
    }

    @Override
    public int getItemCount() {
        return myDataset.size();
    }
}

Finally, you can set up the RecyclerView in your activity or fragment:

recyclerview = findViewById(R.id.recyclerview);
recyclerview.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
adapter = new MyAdapter(myDataset);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(adapter);

Output

Your app should display a list of items with the UI defined in item_layout.xml.

Explanation

RecyclerView is a newer and more efficient way of displaying lists of items in Android apps compared to ListView. RecyclerView allows for better performance by only creating and binding views that are visible on the screen, as the user scrolls through the list.

To use RecyclerView, you need to create a layout file for each item in the list (item_layout.xml in the example above), and a separate adapter class (MyAdapter in the example) that manages the data and binds it to the views.

In your activity or fragment, you can set up the RecyclerView with a layout manager (e.g. LinearLayoutManager or GridLayoutManager) and the adapter that you created.

Use

You can use RecyclerView to display lists of items in your Android apps. RecyclerView is especially useful for large lists that would be slow to load and scroll using ListView.

Important Points

  • RecyclerView requires two main components: the layout file for each item in the list, and the adapter class that manages the data
  • RecyclerView provides better performance compared to ListView by only creating and binding views that are visible on the screen, as the user scrolls through the list
  • RecyclerView can be set up with a variety of layout managers, such as LinearLayoutManager or GridLayoutManager
  • RecyclerView can handle item animations and item decorations

Summary

RecyclerView is a more efficient way of displaying lists of items in Android apps compared to ListView. RecyclerView requires a layout file for each item in the list, and an adapter class that manages the data and binds it to the views. RecyclerView provides better performance by only creating and binding views that are visible on the screen, and can be set up with various layout managers.

Published on: