我在片段中有一个回收器视图,每个回收器视图行项都有一个按钮,因此当用户单击按钮时,它应该打开BottomSheetDialogFragment
,该特定行项的详细信息应该在BottomSheetDialogFragment中可见。
到目前为止,我所做的对话框片段在单击按钮时可以正确显示,但我不知道如何将数据从适配器类传递到对话框片段。
下面是我的代码:
事实适配器.java
public class FactsAdapter extends RecyclerView.Adapter<FactsAdapter.ViewHolder> {
List<Facts> factList;
Context context;
public FactsAdapter(List<Facts> factList, Context context) {
this.factList = factList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.facts_row,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Facts model = factList.get(position);
final String str1 = model.getDescription();
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.color.place);
holder.title.setText(model.getTitle());
Glide.with(context).load(model.getImage()).apply(requestOptions).into(holder.factImage);
holder.more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.show(((FragmentActivity) context).getSupportFragmentManager(),bottomSheet.getTag());
}
});
}
@Override
public int getItemCount() {
return factList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView factImage;
TextView title;
Button more;
public ViewHolder(@NonNull View itemView) {
super(itemView);
factImage = itemView.findViewById(R.id.factImage);
title = itemView.findViewById(R.id.title);
more = itemView.findViewById(R.id.more);
}
}
}
如何将数据传递给对话框片段?
2条答案
按热度按时间vshtjzan1#
测试它
转接器:
底部片材片段:
xpszyzbs2#
如果我没猜错的话,你可以试着做一个接口:
然后在Fragment/Activity/BottomSheetDialogFragment中实现此接口,并将其作为侦听器传递给适配器的构造函数:
现在,您可以轻松地在需要的任何地方调用此侦听器,并在Fragment/Activity/BottomSheetDialogFragment中获取所需的数据,如下所示: