Reputation: 13
The recyclers child view contains two click-able objects , one is a set of TextViews and ImageView, the other one is a CheckBox (Refer - Android Recyclerview Multiple onclick items) I want to see if on the child the checkbox is clicked or not. If so, then I change the state of the CheckBox. Else, I initiate another activity (for result). The above link shows handling clicks IN the view holder. I would like to handle clicks in one of my other activities where I am setting up the RecyclerView.
//the view needs a listener
final GestureDetector mGestureDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && mGestureDetector.onTouchEvent(motionEvent)){
if(child.getId() == R.id.chk_box_pin){
//checking, this didn't work
}
else {
TextView idView = (TextView) child.findViewById(R.id.note_id_txt_vw);
if (DEBUG) Toast.makeText(getActivity(),
"ID is " + idView.getText().toString(), Toast.LENGTH_SHORT).show();
startEditNoteActivity(ActivityContract.ACTIVITY_EDIT, idView.getText().toString());
}
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
});
Is there a way I could handle clicks on multiple items inside a child in an activity (instead of the ViewHolder) ?
Upvotes: 1
Views: 7769
Reputation: 124
Unfortunately, you have to implement your own listener to communicate with your activity.
Create an interface, implement it on your activity, pass your interface instance on your adapter (cast the context on your interface) and call your custom method with custom parameters.
Like this you should have access to your ViewHolder
(don't forget to use the adapter best practice implementation).
Hope it's helps !
Best.
Check this: Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?
Upvotes: 1
Reputation: 251
You can always create an interface which will pass the event to activity. So for example
public interface ChildItemClickListener {
public void onClick(View v, int position);
}
Then in your adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
/* your code*/
...
private ChildItemClickListener listener;
...
*/
public void setChildItemClickListener(ChildItemClickListener listener) {
this.listener = listener;
}
}
then you invoke it like this
if(child!=null && mGestureDetector.onTouchEvent(motionEvent)) {
if(child.getId() == R.id.chk_box_pin){
//checking, this didn't work
}
else {
TextView idView = (TextView) child.findViewById(R.id.note_id_txt_vw);
if (DEBUG) Toast.makeText(getActivity(),
"ID is " + idView.getText().toString(), Toast.LENGTH_SHORT).show();
startEditNoteActivity(ActivityContract.ACTIVITY_EDIT, idView.getText().toString());
}
if(listener != null)
listener.onClick(YOUR_VIEW, POSITION);
return true;
}
and that would be it :) hope it helps, good luck
Upvotes: 6