I am trying to add RecyclerItemClickListener on my RecyclerView. It works fine, thing is my 2 components that are in my RecyclerView needs to be double clicked to execute their onClick action.
Here is my recyclerView.addOnItemTouchListener which is in my MainActivity:
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, final int pos) {
TextView sourceName = (TextView) view.findViewById(R.id.sourceName);
ImageView moreSettings = (ImageView) view.findViewById(R.id.moreSettings);
// Add on click listeners
sourceName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initiateDialogAlert(passObjArrayList.get(pos).getName(), passObjArrayList.get(pos).getPassword());
boiteAlerte.show();
}
});
moreSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createPopupMenu(view, pos);
}
});
}
});
How do I prevent this behaviour of needing to click twice on a component to execute his action?
My RecyclerItemClickListener:
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;
}
public static boolean isViewClicked(View view, MotionEvent e) {
Rect rect = new Rect();
view.getGlobalVisibleRect(rect);
return rect.contains((int) e.getRawX(), (int) e.getRawY());
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
See there is difference between onTouch and onClick. And for recyclerview when you are handling onClick you have be code wisely.
Recyclerview don't give the functionality to us onClickListener by default. So you can create an interface and perform it.
Please refer below example to use onClickListener for recyclerview.
https://stand2code.blogspot.in/2016/12/recyclerview-item-onclicklistener.html
And you are returning true when your view is single tapped. Make sure that is this the problem maker or not ?
And if you want to perform different actions for different view onClicks. Try different interfaces there. It will help you from causing problems.
Sample Code :