Created
May 20, 2023 11:02
-
-
Save magdamiu/494132527294c20841445b8b849f364b to your computer and use it in GitHub Desktop.
RecyclerView Click and LongClick Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// STEP 1 - create new file | |
public interface ClickListener { | |
public void onClick(View view, int position); | |
public void onLongClick(View view, int position); | |
} | |
// STEP 2 - create new file | |
import android.content.Context; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import androidx.recyclerview.widget.RecyclerView; | |
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { | |
private ClickListener clickListener; | |
private GestureDetector gestureDetector; | |
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) { | |
this.clickListener = clickListener; | |
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapUp(MotionEvent motionEvent) { | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent motionEvent) { | |
View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); | |
if (child != null && clickListener != null) { | |
clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child)); | |
} | |
} | |
}); | |
} | |
@Override | |
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { | |
View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); | |
if (child != null && clickListener != null && gestureDetector.onTouchEvent(motionEvent)) { | |
clickListener.onClick(child, recyclerView.getChildAdapterPosition(child)); | |
} | |
return false; | |
} | |
@Override | |
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { | |
} | |
@Override | |
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { | |
} | |
} | |
// STEP 3 - include this method where you handle the RecyclerView (activity or fragment) | |
// make sure you call the method in the onCreate() (for activity) or onCreateView() (for fragment) | |
private void handleRecyclerViewItemClick() { | |
recyclerViewPlanets.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerViewPlanets, new ClickListener() { | |
@Override | |
public void onClick(View view, final int position) { | |
Toast.makeText(PlanetsActivity.this, "Single Click on position:" + position, | |
Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onLongClick(View view, int position) { | |
Toast.makeText(PlanetsActivity.this, "Long press on position:" + position, | |
Toast.LENGTH_LONG).show(); | |
// here you could also start a new activity with Intent | |
// and pass the id of the current selected object using putExtra (Bundle) | |
Planet currentPlanet = planets.get(position); | |
// currentPlanet.getId(); | |
} | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment