Android - RecyclerView: Implementing single item click and long press (Part-II)

Prerequisite

This article is a continuation, please read through Implementing ReyclerView

Creating Interface

I hope you have read my previous article. Let me take you from there. Under MainActivity, We need to create a simple interface for single tap and for long press events, by passing “view” and its “position” as parameters

public static interface ClickListener{
public void onClick(View view,int position);
public void onLongClick(View view,int position);
}

Creating InnerClass

Under MainActivity, Let’s create an inner class implementing RecyclerView.OnItemTouchListener. We also need to make use of ClickListener interface in this inner class. GestureDetector class is used to listen for various touch events. To learn more about GestureDetector, please refer this link

class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{

private ClickListener clicklistener;
private GestureDetector gestureDetector;

public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener){

this.clicklistener=clicklistener;
gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {
View child=recycleView.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null){
clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
}
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child=rv.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
clicklistener.onClick(child,rv.getChildAdapterPosition(child));
}

return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
}

Adding Click Listeners to RecyclerView

Now let’s add the ItemTouchListener to the RecyclerView, where we will pass RecyclerTouchListener class. This will implement onClick and onLongClick methods.These methods will listen for the click and long press events on the particular position of the view.

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
//Values are passing to activity & to fragment as well
Toast.makeText(MainActivity.this, "Single Click on position :"+position,
Toast.LENGTH_SHORT).show();
}

@Override
public void onLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "Long press on position :"+position,
Toast.LENGTH_LONG).show();
}
}));

So….. our Listeners are all ready. Let’s run our project.

Et voila !! Here is our output !

Single click on First Item (position 0)
Long press on the first Item (position 0)

Bonus Information :

Accessing Particular View Component in the RecyclerView Item

In order to access particular view component inside RecyclerView, for example, In order to perform some operation only on clicking the profile image in the RecyclerView row item, then here is the small hack for you.

We can define the component by findViewById via it’s respective View. Here is the code for you.

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
//Values are passing to activity & to fragment as well
Toast.makeText(MainActivity.this, "Single Click on position :"+position,
Toast.LENGTH_SHORT).show();
ImageView picture=(ImageView)view.findViewById(R.id.picture);
picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Single Click on Image :"+position,
Toast.LENGTH_SHORT).show();
}
});

}

@Override
public void onLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "Long press on position :"+position,
Toast.LENGTH_LONG).show();
}
}));

Now you can perform any operation on clicking the image. Follow the same for the other view components.

Implementing Click Events for Fragment

For beginners, In order to implement the single tap and long press event listeners within a fragment, you need to pass the right context. Using this within fragments, will not allow the fragment to know which activity context you are passing. So solution is to replace this/MainActivity.this with getActivity().

Fork me on GitHub

Check out the complete source code in the following link: https://github.com/Hariofspades/CustomRecyclerView

Happy Coding !!

Hari Vignesh Jayapalan

Written by

Android Dev at Backbase | #1 Google AAD | Public Speaker | Blogger | Creative Coder & Logical Designer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade