Advanced onClick RecyclerView using Interface | RecyclerView item Click using Interface
By: Golap Gunjan Barman
Tags: Android App Development, Android, AndroidDev, Recyclerview, Android Recyclerview

In this tutorial, we’re going to create a recyclerView and apply on Click on the recyclerView using Interface. This the advanced way to apply items click on the recyclerView.

Before adding the interface first create recyclerView. Here I’m not going to explain how to create a recyclerView, if you don’t know how to create a recyclerView then go to my previous tutorial on recyclerView How to create RecyclerView in Android.

And if you’re familiar with it then just follow these steps to create recyclerView or we can create your own recyclerView.

Before applying the Interface callbacks

activity_main.xml

<?xml version=”1.0" encoding=”utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android" xmlns:app=”http://schemas.android.com/apk/res-auto" xmlns:tools=”http://schemas.android.com/tools" android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity”> <androidx.recyclerview.widget.RecyclerView android:id=”@+id/recyclerView” android:layout_width=”match_parent” android:layout_height=”match_parent” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toTopOf=”parent” /> </androidx.constraintlayout.widget.ConstraintLayout>

item_row.xml

<?xml version=”1.0" encoding=”utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android" xmlns:app=”http://schemas.android.com/apk/res-auto" xmlns:tools=”http://schemas.android.com/tools" android:layout_width=”match_parent” android:layout_height=”wrap_content” android:padding=”5dp” android:background=”?attr/selectableItemBackground”> <ImageView android:id=”@+id/imageView” android:layout_width=”48dp” android:layout_height=”48dp” android:layout_marginStart=”16dp” android:layout_marginTop=”16dp” android:layout_marginBottom=”16dp” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintTop_toTopOf=”parent” app:srcCompat=”@android:drawable/btn_star_big_on” /> <TextView android:id=”@+id/textView” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_marginStart=”16dp” android:fontFamily=”@font/poppins_semibold” android:text=”TextView” android:textSize=”16sp” android:textColor=”@android:color/black” app:layout_constraintBottom_toTopOf=”@+id/rowCount” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toEndOf=”@+id/imageView” app:layout_constraintTop_toTopOf=”parent” /> <TextView android:id=”@+id/rowCount” android:layout_width=”0dp” android:layout_height=”wrap_content” android:fontFamily=”@font/poppins_light” android:text=”TextView” android:textSize=”14sp” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintEnd_toEndOf=”parent” app:layout_constraintStart_toStartOf=”@+id/textView” app:layout_constraintTop_toBottomOf=”@+id/textView” /> </androidx.constraintlayout.widget.ConstraintLayout>

RecyclerViewAdapter.java

import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{ private static final String TAG = “RecyclerAdapter”; List<String> moviesList; public RecyclerAdapter(List<String> moviesList) { this.moviesList = moviesList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View view = layoutInflater.inflate(R.layout.row_item, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.rowCountTextView.setText(String.valueOf(position)); holder.textView.setText(moviesList.get(position)); } @Override public int getItemCount() { return moviesList.size(); } class ViewHolder extends RecyclerView.ViewHolder implement View.OnClickListener { ImageView imageView; TextView textView, rowCountTextView; public ViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.imageView); textView = itemView.findViewById(R.id.textView); rowCountTextView = itemView.findViewById(R.id.rowCount); itemView.setOnClickListener(new View.OnClickListener(this); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { moviesList.remove(getAdapterPosition()); notifyItemRemoved(getAdapterPosition()); return true; } }); } @Override public void onClick(View view){ Toast.makeText(view.getContext(), moviesList.get(getAdapterPosition()), Toast.LENGTH_SHORT).show(); } } }

MainActivity.java

import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import android.os.Bundle; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; RecyclerAdapter recyclerAdapter; List<String> moviesList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); moviesList = new ArrayList<>(); recyclerView = findViewById(R.id.recyclerView); recyclerAdapter = new RecyclerAdapter(moviesList); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(recyclerAdapter); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); recyclerView.addItemDecoration(dividerItemDecoration); moviesList.add(“Captain America”); moviesList.add(“The Fantastic Four”); moviesList.add(“Howard the Duck”); moviesList.add(“Blade”); moviesList.add(“X-Men”); moviesList.add(“Spider-Man”); moviesList.add(“Daredevil”); moviesList.add(“Hulk”); moviesList.add(“The Punisher”); moviesList.add(“Ghost Rider”); moviesList.add(“Iron Man”); moviesList.add(“Thor”); moviesList.add(“The Avengers”); moviesList.add(“Guardians of the Galaxy”); moviesList.add(“Ant-Man”); moviesList.add(“Deadpool”); moviesList.add(“Doctor Strange”); moviesList.add(“Logan”); moviesList.add(“Black Panther”); moviesList.add(“Venom”); moviesList.add(“Dark Phoenix”); moviesList.add(“The New Mutants”); } }

After applying the Interface callbacks

You don’t need to change the main and item row XML files, they are same as it is. We need to make some change in the Adapter class and Main activity java files, also we need to implement the interface callbacks in our main activity for item clicks inside recyclerView.

For that first create an interface. Right-click on the java package and create a new java file, but make sure kind is to be an interface .

is to be an . Inside here we create some methods that we would like to handle inside our main activity that when a person clicks on an item this method would be called.

RecyclerViewClickInterface.java

public interface RecyclerViewClickInterface { void onItemClick(int position); void onLongItemClick(int position); }

Now let’s go to our MainActivity.java and implement the interface that we just created which was the recyclerView click interface.

and implement the interface that we just created which was the recyclerView click interface. Let’s go to the top of our class that is the Main Activity class it extends from AppCompatActivity but also implements our interface which we have just created.

public class MainActivity extends AppCompatActivity implements RecyclerViewClickInterface after that we need to implement those two methods that we are created on our interface. @Override public void onItemClick(int position) { } @Override public void onLongItemClick(int position) { }

Now go to the RecyclerView Adapter class and create a variable for our interface and also change the constructor of the RecyclerAdapter.

private RecyclerViewClickInterface recyclerViewClickInterface; public RecyclerAdapter(List<String> moviesList, RecyclerViewClickInterface recyclerViewClickInterface) { this.moviesList = moviesList; this.recyclerViewClickInterface = recyclerViewClickInterface; }

Now inside our main java class where we called our adapter, it now needs one more argument that is the interface.

recyclerAdapter = new RecyclerAdapter(moviesList, this);

Here, this means the interface.

Now go to the adapter class where we set the item view set onClickListener, instead of passing it along with the item view now we pass the reference of the interface so that we can handle the on click from the main activity java file.

Make these changes in the Adapter class and Main Activity java file.

RecyclerViewAdapter.java

import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{ private static final String TAG = “RecyclerAdapter”; List<String> moviesList; private RecyclerViewClickInterface recyclerViewClickInterface; public RecyclerAdapter(List<String> moviesList, RecyclerViewClickInterface recyclerViewClickInterface) { this.moviesList = moviesList; this.recyclerViewClickInterface = recyclerViewClickInterface; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View view = layoutInflater.inflate(R.layout.row_item, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.rowCountTextView.setText(String.valueOf(position)); holder.textView.setText(moviesList.get(position)); } @Override public int getItemCount() { return moviesList.size(); } class ViewHolder extends RecyclerView.ViewHolder { ImageView imageView; TextView textView, rowCountTextView; public ViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.imageView); textView = itemView.findViewById(R.id.textView); rowCountTextView = itemView.findViewById(R.id.rowCount); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { recyclerViewClickInterface.onItemClick(getAdapterPosition()); } }); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { // moviesList.remove(getAdapterPosition()); // notifyItemRemoved(getAdapterPosition()); recyclerViewClickInterface.onLongItemClick(getAdapterPosition()); return true; } }); } } }

MainActivty.java

import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import android.os.Bundle; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements RecyclerViewClickInterface{ RecyclerView recyclerView; RecyclerAdapter recyclerAdapter; List<String> moviesList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); moviesList = new ArrayList<>(); recyclerView = findViewById(R.id.recyclerView); recyclerAdapter = new RecyclerAdapter(moviesList, this); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(recyclerAdapter); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); recyclerView.addItemDecoration(dividerItemDecoration); moviesList.add(“Captain America”); moviesList.add(“The Fantastic Four”); moviesList.add(“Howard the Duck”); moviesList.add(“Blade”); moviesList.add(“X-Men”); moviesList.add(“Spider-Man”); moviesList.add(“Daredevil”); moviesList.add(“Hulk”); moviesList.add(“The Punisher”); moviesList.add(“Ghost Rider”); moviesList.add(“Iron Man”); moviesList.add(“Thor”); moviesList.add(“The Avengers”); moviesList.add(“Guardians of the Galaxy”); moviesList.add(“Ant-Man”); moviesList.add(“Deadpool”); moviesList.add(“Doctor Strange”); moviesList.add(“Logan”); moviesList.add(“Black Panther”); moviesList.add(“Venom”); moviesList.add(“Dark Phoenix”); moviesList.add(“The New Mutants”); } @Override public void onItemClick(int position) { Toast.makeText(this, moviesList.get(position), Toast.LENGTH_SHORT).show(); } @Override public void onLongItemClick(int position) { Toast.makeText(this, moviesList.get(position) + “ Removed”, Toast.LENGTH_SHORT).show(); moviesList.remove(position); recyclerAdapter.notifyItemRemoved(position); } }

Output

The output is not different from regular on click. Here we just apply interface callbacks for the item clicks.