1

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) ?

Community
  • 1
  • 1
Aniket
  • 13
  • 1
  • 5

2 Answers2

6

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

bartol
  • 251
  • 1
  • 12
  • I have upvoted it but this won't help to get you manage the clicks of child item of RecyclerView from activity/fragment instead of Adapter. Suppose i have imageButton as a child and i have set clickListeners for both views (parent - whole row and child - imageButton) on clicking child it will fire events of both as both methods are taking View as input param, so whole view gets clicked alongside imageButton(child). – mfaisalhyder Jun 08 '16 at 07:21
  • 1
    I have found the way to make both work from mainactivity rather than from adapter :) in activity just check for the view.getId()= R.id.btnid inside if block and do what that button click has to do and in else block do what the whole row click has to do . :) – mfaisalhyder Jun 08 '16 at 09:09
1

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?

Community
  • 1
  • 1
  • Thanks for the reply!! It helps, but I am not sure how you would want me to call stuff back on the activity X (where I have defined the recyclerview click listeners). I know it can be done if I migrate most of the code off to the adapter, but that would break out codes modularity. And we don't want that :) Could you elaborate with a small example? That would be super. – Aniket May 11 '15 at 19:24