Fast taps (clicks) on RecyclerView can cause two situations-
A single item of RecyclerView clicked multiple times.
This can cause the destination fragment to be created multiple times thereby making a single fragment stack up multiple times, disrupting the user's smooth experience.
Multiple items of the RecyclerView being clicked at a time.
This can cause the undesired behavior of the app. (Opening multiple fragments again.)
Both situations should be handled for a proper app running experience.
To prevent 1st situation you can use the logic that within a definite interval of time if the item is clicked more than once then it should not go for creating a new fragment.
Here is the code below-
class ViewHolder extends RecyclerView.ViewHolder{
//Suppose your item is a CardView
private CardView cardView;
private static final long TIME_INTERVAL_GAP=500;
private long lastTimeClicked=System.currentTimeMillis();
public ViewHolder(@NonNull View itemView)
{
cardView=itemView.findViewById(R.id.card_view);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long now=System.currentTimeMillis();
//check if cardView is clicked again within the time interval gap
if(now-lastTimeClicked<TIME_INTERVAL_GAP)
return; //no action to perform if it is within the interval gap.
mLastClickTime=now;
//... Here your code to open a new fragment
}
});
}
}
Solution for 2nd scenario-
RecyclerView has a method setMotionEventSplittingEnabled(boolean split)
The documentation says-
Enable or disable the splitting of MotionEvents to multiple children during touch event dispatch. This behavior is enabled by default for applications that target an
SDK version of HONEYCOMB or newer.
When this option is enabled MotionEvents may be split and dispatched to different child views depending on where each pointer initially went down. This allows for user interactions such as scrolling two panes of content independently, chording of buttons and performing independent gestures on different pieces of content. Split set to true to allow MotionEvents to be split and dispatched to multiple child views. and set it to false to only allow one child view to be the target of.
So in your code just add a line of code-
recyclerView.setMotionEventSplittingEnabled(false);
These will surely solve the problem caused due to fast taps on the RecyclerView and prevent your app to stack up the same fragment unnecessarily.