
In this Tutorial to implement RecyclerView Custom Item Click Listener in adapter.it is single click listener and long click listener in adapter to customization.
we can create interface and declared methods on interface and this interface used to recyclerView adapter click listener. and you can declared adapter object to Activity and you can used this interface click event method in interface.
I have simple create recyclerView and then create simple recyclerView adapter and then create click listeners.you can create long press click event.
ItemClickListener in RecyclerView Example
Create activity_main.xml file And Follow this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.bhaumik.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_item" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
Create listener_item.xml Layout File Follow this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_listener_item" android:layout_width="match_parent" android:layout_height="100dp" android:text="Hello World" android:textSize="@dimen/_30dp" android:gravity="center" /> </LinearLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.example.bhaumik; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast; import com.example.bhaumik.ListenerAdapter; import com.example.bhaumik.ItemListener; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { String[] items= {"Aestro","Blender","Cupcake","Donut","Eclair","Froyo","GingerBread" ,"HoneyComb", "IceCream Sendwich","JellyBean","Kitkat","Lolipop","Marshmallow","Noght"}; List<String> list = new ArrayList<>(); ListenerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view_item_listener); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_item); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); int i=0; for (String data : items){ list.add(data); i++; } adapter = new ListenerAdapter(list); recyclerView.setAdapter(adapter); adapter.setOnItemClickListener(new ItemListener() { @Override public void onItemClick(int pos) { Toast.makeText(RecyclerViewItemListenerActivity.this,"Clicked Item Is : " + list.get(pos), Toast.LENGTH_SHORT).show(); } }); } } |
Create ListenerAdapter.java File follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.example.bhaumik; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.bhaumik.R; import com.example.bhaumik.ItemListener; import java.util.ArrayList; import java.util.List; public class ListenerAdapter extends RecyclerView.Adapter<ListenerAdapter.ListenerHolder>{ List<String> list = new ArrayList<>(); ItemListener itemListener; public ListenerAdapter(List<String> list) { this.list = list; } @Override public ListenerHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listener_item,parent,false); return new ListenerHolder(view); } @Override public void onBindViewHolder(ListenerHolder holder, final int position) { holder.textView.setText(list.get(position)); holder.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { itemListener.onItemClick(position); } }); } @Override public int getItemCount() { return list.size(); } public void setOnItemClickListener(ItemListener itemListener){ this.itemListener = itemListener; } public static class ListenerHolder extends RecyclerView.ViewHolder{ TextView textView; public ListenerHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_listener_item); } } } |
Create Interface ItemListener.java file Follow this Code.
1 2 3 4 5 6 |
package com.example.bhaumik; public interface ItemListener { void onItemClick(int pos); } |

In this Example Create a simple RecyclerView and Adapter and then create interface in ItemListener.java file. in this file used to ListenerAdapter and Create object of interface and then create setOnItemClickListener() Method and pass ItemListener object.
Interface method is used to Click Listener event in adapter and then Created method on setOnItemClickListener() Called MainActivity.java File use before the create adapter object.It can Initialize to adapter object then method name you want to create in adapter and then implement of override method in Interface. then this method to call when user can click on item to display message.