Android RecyclerView example

Tutorial

The RecyclerView class expands the ViewGroup class and actualizes ScrollingView interface. It is presented in Marshmallow. It is a propelled adaptation of the ListView with improved execution and different advantages. RecyclerView is for the most part used to structure the UI with the fine-grain authority over the rundowns and lattices of android application.

Let’s Start

Update Gradle dependencies

implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'

Create activity layout

activity_main

<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>

Create item layout

In our RecyclerView, each item will have only one TextView. Create a resource file for a new layout.

item_recyclerview.xml

<?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="10dp">

<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>

</LinearLayout>

Create the adapter

The RecyclerView requires an adapter to fill in the opinions with your information in each row. Create a new java class

Ad_RecyclerView.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Ad_RecyclerView extends RecyclerView.Adapter<Ad_RecyclerView.ViewHolder> {

private List<String> mData;
private LayoutInflater mInflater;
private Context mContext;

// data is passed into the constructor
Ad_RecyclerView(Context context, List<String> data) {
this.mContext = context;
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_recyclerview, parent, false);
return new ViewHolder(view);
}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
holder.myTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "item clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
}

// total number of items
@Override
public int getItemCount() {
return mData.size();
}

// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder {
TextView myTextView;

ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.text_view);
}
}

}

Initialize RecyclerView in Activity

Add the following code to your main activity.

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Ad_RecyclerView ad_recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // dummy  data
        ArrayList<String> list = new ArrayList<>();
        list.add("item1");
        list.add("item2");
        list.add("item3");
        list.add("item4");
        list.add("item5");

        // set up the RecyclerView
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ad_recyclerView = new Ad_RecyclerView(this, list);
        recyclerView.setAdapter(ad_recyclerView);
    }
}

That’s it. Get the full example Github

Leave a Reply

×