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);
}
}
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With