Skip to content

Instantly share code, notes, and snippets.

@douglasdrumond
Created March 28, 2016 06:45
Show Gist options
  • Save douglasdrumond/e0a2eb4182acebf77d2d to your computer and use it in GitHub Desktop.
Save douglasdrumond/e0a2eb4182acebf77d2d to your computer and use it in GitHub Desktop.
A click / long press listener for RecyclerViews. Questions? https://twitter.com/lnikkila

RecyclerItemClickListener

Questions? Comments?
https://twitter.com/lnikkila


Handles clicks and long presses out of the box.

Use it like this:

public class SampleActivity extends Activity
        implements RecyclerItemClickListener.OnItemClickListener {

    @Override
    protected void onStart() {
        // ...

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, this));

        // ...
    }

    @Override
    public void onItemClick(View childView, int position) {
        // Do something when an item is clicked.
    }

    @Override
    public void onItemLongPress(View childView, int position) {
        // Do another thing when an item is long pressed.
    }

}

Or like this:

public class SampleActivity extends Activity {

    @Override
    protected void onStart() {
        // ...

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this,
                new OnItemClickListener()));

        // ...
    }

    private class OnItemClickListener extends RecyclerItemClickListener.SimpleOnItemClickListener {

        @Override
        public void onItemClick(View childView, int position) {
            // Do something when an item is clicked, or override something else.
        }

    }

}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
/**
* Used for listening to RecyclerView item clicks. You can either implement an OnItemClickListener
* or extend SimpleOnItemClickListener and override its methods.
*
* Licence: MIT
*
* @author Leo Nikkilä <[email protected]>
*/
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
protected OnItemClickListener listener;
private GestureDetector gestureDetector;
@Nullable
private View childView;
private int childViewPosition;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
this.gestureDetector = new GestureDetector(context, new GestureListener());
this.listener = listener;
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent event) {
childView = view.findChildViewUnder(event.getX(), event.getY());
childViewPosition = view.getChildPosition(childView);
return childView != null && gestureDetector.onTouchEvent(event);
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent event) {
// Not needed.
}
/**
* A click listener for items.
*/
public interface OnItemClickListener {
/**
* Called when an item is clicked.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/
public void onItemClick(View childView, int position);
/**
* Called when an item is long pressed.
*
* @param childView View of the item that was long pressed.
* @param position Position of the item that was long pressed.
*/
public void onItemLongPress(View childView, int position);
}
/**
* A simple click listener whose methods can be overridden one by one.
*/
public static abstract class SimpleOnItemClickListener implements OnItemClickListener {
/**
* Called when an item is clicked. The default implementation is a no-op.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/
public void onItemClick(View childView, int position) {
// Do nothing.
}
/**
* Called when an item is long pressed. The default implementation is a no-op.
*
* @param childView View of the item that was long pressed.
* @param position Position of the item that was long pressed.
*/
public void onItemLongPress(View childView, int position) {
// Do nothing.
}
}
protected class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
if (childView != null) {
listener.onItemClick(childView, childViewPosition);
}
return true;
}
@Override
public void onLongPress(MotionEvent event) {
if (childView != null) {
listener.onItemLongPress(childView, childViewPosition);
}
}
@Override
public boolean onDown(MotionEvent event) {
// Best practice to always return true here.
// http://developer.android.com/training/gestures/detector.html#detect
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment