这是我的适配器代码,我想将数据传递给“ParticularFragment” 注意:ParticularFragment扩展了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);
}
}
}
答案 0 :(得分:8)
在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();
注意:更改
private Context context;
到
private Activity context;
并且作为TopicsAdapter构造函数的参数从您的片段传递getActivity()。