Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView onClick

Has anyone using RecyclerView found a way to set an onClickListener to items in the RecyclerView? I thought of setting a listener to each of the layouts for each item but that seems a little too much hassle I'm sure there is a way for the RecyclerView to listen for the onClick event but I can't quite figure it out.

like image 608
CurtJRees Avatar asked Jun 28 '14 21:06

CurtJRees


People also ask

When using RecyclerView you should handle clicks in the?

@veritas because in accepted answer you have to set click listener every time when you bind view holder, in this case you set touch listener for RecyclerView in one place and that's it.

Where do you add the Android onClick attribute to make items in a RecyclerView respond to clicks?

Where do you add the android:onClick attribute to make items in a RecyclerView respond to clicks? In the layout file that displays the RecyclerView, add it to the element. Add it to the layout file for an item in the row.


1 Answers

Here is a better and less tightly coupled way to implement an OnClickListener for a RecyclerView.

Snippet of usage:

RecyclerView recyclerView = findViewById(R.id.recycler); recyclerView.addOnItemTouchListener(     new RecyclerItemClickListener(context, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {       @Override public void onItemClick(View view, int position) {         // do whatever       }        @Override public void onLongItemClick(View view, int position) {         // do whatever       }     }) ); 

RecyclerItemClickListener implementation:

import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View;   public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {   private OnItemClickListener mListener;    public interface OnItemClickListener {     public void onItemClick(View view, int position);      public void onLongItemClick(View view, int position);   }    GestureDetector mGestureDetector;    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {     mListener = listener;     mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {         @Override         public boolean onSingleTapUp(MotionEvent e) {             return true;         }          @Override         public void onLongPress(MotionEvent e) {             View child = recyclerView.findChildViewUnder(e.getX(), e.getY());             if (child != null && mListener != null) {                 mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));             }         }     }); }    @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {     View childView = view.findChildViewUnder(e.getX(), e.getY());     if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {       mListener.onItemClick(childView, view.getChildAdapterPosition(childView));       return true;     }     return false;   }    @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }    @Override   public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){} } 
like image 174
Jacob Tabak Avatar answered Sep 27 '22 19:09

Jacob Tabak