Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to fragment when click on cardview in onBindViewHolder?

Here is my adapter code and I want to pass data to the "ParticularFragment" Note : ParticularFragment extends Fragment

public class TopicsAdapter extends RecyclerView.Adapter<TopicsAdapter.ViewHolder> {

    private Context context;
    private ArrayList<QuoteItems> itemList;

    public TopicsAdapter(Context context, ArrayList<QuoteItems> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    @Override
    public TopicsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_topics_recycler_view_items, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final TopicsAdapter.ViewHolder viewHolder, int i) {

        final QuoteItems quoteItems = itemList.get(i);
        viewHolder.txtQuote.setText(quoteItems.getQuote());

        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             //how to pass data to "ParticularFragment" from here
            }
        });
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtQuote;
        public CardView cardView;

        public ViewHolder(View itemView) {
            super(itemView);
            txtQuote = (TextView) itemView.findViewById(R.id.txtQuote);
            cardView = (CardView) itemView.findViewById(R.id.cardView);
        }
    }
}
like image 405
Pratik Surela Avatar asked Aug 03 '15 05:08

Pratik Surela


People also ask

What is the difference between onCreateViewHolder and onBindViewHolder?

This method internally calls onBindViewHolder to update the ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView. This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView.

What is viewType in onCreateViewHolder?

This viewType variable is internal to the Adapter class. It's used in the onCreateViewHolder() and onBindViewHolder to inflate and populate the mapped layouts. Before we jump into the implementation of the Adapter class, let's look at the types of layouts that are defined for each view type.

How to bind data in RecyclerView in android?

Code. The layout for each row of the RecyclerView is defined in item_row. xml . In order to pass the data to the XML counterpart we bind it using itemRowBinding.


1 Answers

Add following code in your onclick

ReciverFragment fragment = new ReciverFragment(); 
Bundle args = new Bundle();
args.putString("Your_Key", "Your_Value");
fragment.setArguments(args);
//Inflate the fragment
context.getFragmentManager().beginTransaction().add(R.id.container, fragment).commit();

Note: change

private Context context;

to

private Activity context;

and as parameter of TopicsAdapter constructor pass getActivity() from your fragment.

like image 169
Android Team Avatar answered Oct 09 '22 18:10

Android Team