paul590
paul590

Reputation: 1425

Recycler View onClick

I have an activity that creates a tab layout inside of it. The layout contains two fragments. The first fragment has a recycler view. Each card in the recycler view has a check box and a string.

If I click on the checkbox I want to send this card's string to a List located in the activity so I can populate it onto the second tab fragment.

On the first fragment cards I have an on click that sets boolean to true which is saved on an object. I am trying to figure out how to grab this object when it is clicked and send it to the activities list.

Activity-
public class MainActivity extends AppCompatActivity {
public static List toSendList = new ArrayList();
...more code
}


Recycler Adapter-
//initialize variables...
CheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (CheckBox.isChecked()) {
                //MainActivity.toSendList.add(Obj);
                obj.setIsChecked(true);
            }
        }
    });
... more code

Upvotes: 0

Views: 306

Answers (3)

Aldi Renaldi Gunawan
Aldi Renaldi Gunawan

Reputation: 510

try this :

Activity-
public class MainActivity extends AppCompatActivity {
public static List toSendList = new ArrayList();
...more code
  public setPassData(String string)
  {
    //do something with this string
  }
}

Fragment A/B
public void setPassData(String string)
{
   ((ActivityName)getActivity()).setPassData(string);
}


Recycler Adapter-
//initialize variables...
CheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (CheckBox.isChecked()) {
                //MainActivity.toSendList.add(Obj);
                obj.setIsChecked(true);
                ((FragmentName)mContext).setPassData(SomeStringToPass);
            }
        }
    });

Upvotes: 1

lithiumsheep
lithiumsheep

Reputation: 254

You might try EventBus and see if you like it. With it, you define an 'event' which can also be the object that you want to pass to your activity (or an event which wraps it). In the receiving activities, add your Subcribers. You then pass events/objects by calling

EventBus.getDefault().post(new CustomEvent());

and any place where a Subcriber is still attached will receive the event.

Upvotes: 2

thyago stall
thyago stall

Reputation: 1704

You can implement a listener which you will use on the Activity. For example:

You create a interface for a click listener.

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PhotoHolder> {
    // ... all the code

    public interface RecyclerViewOnClickListener(/* same arguments as above */);

}

On your view holder.

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ViewHolder(View v) {
        super(v);         
        // ... do all the initialization
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mOnClickListener(int position /* or whatever argument you like */)
    }
}

On your RecyclerAdapter you store a listener for the click. So the adapter would end up with more code:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PhotoHolder> {
    // ... all the code

    public interface RecyclerViewOnClickListener(/* same arguments as above */);

    public RecyclerAdapter(RecyclerViewOnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

}

So finally, in your Activity you just instantiate the RecyclerViewOnClickListener and pass it as an argument for the adapter.

Upvotes: 1

Related Questions