我知道此问题已在社区中提出,但仍然无法解决我的问题。这就是为什么再次发布此信息。如何将数据从recyclerview适配器传递到片段。我已成功将数据从适配器传递到活动。但是无法获取如何将数据从适配器传递到片段。我的数据库中有两个参数(1)mSliderImage和(2)mTitle。 mSliderImage显示在主片段上。但是现在我想要当用户单击滑块的特定图像时,打开一个新片段,并显示mTitle。我可以基于图像位置单击图像滑块来成功打开新片段。但没有获得如何在该片段中传递mTitle的信息。请有人帮我整个代码,因为我必须在1月17日之前提交此大学级别的项目。我只发布重要代码,而不是整个代码。我只想编码如何将mSliderTitle从适配器传递到片段的编码解决方案。当我单击图像滑块的某些图像时。
下面是我的代码:
在模型中,我已经通过了上面已经提到的2个Getters和Setters。 (1)mSliderImage和(2)mTitle。
滑块适配器:
public class SliderImageAdapter extends SliderViewAdapter<SliderImageAdapter.SliderAdapterVH>{
public List<Banner> bannerList;
public Context context;
private OnItemClicked onClick;
public interface OnItemClicked {
void onItemClick(int position);
}
public SliderImageAdapter(Context context, List<Banner> bannerList, OnItemClicked onClick) {
this.bannerList = bannerList;
this.context = context;
this.onClick = onClick;
}
@Override
public SliderAdapterVH onCreateViewHolder(ViewGroup parent) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_slider_myshop, parent, false);
return new SliderAdapterVH(inflate);
}
@Override
public void onBindViewHolder(final SliderAdapterVH viewHolder, final int position) {
Glide.with(viewHolder.itemView)
.load(bannerList.get(position).getmSliderImage())
.into(viewHolder.imageViewBackground);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}
@Override
public int getCount() {
return bannerList.size();
}
public int getItemPosition (Object object) {
return POSITION_NONE;
}
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}
class SliderAdapterVH extends SliderViewAdapter.ViewHolder {
View itemView;
ImageView imageViewBackground;
public SliderAdapterVH(View itemView) {
super(itemView);
imageViewBackground = itemView.findViewById(R.id.iv_auto_image_slider);
this.itemView = itemView;
}
}
主页片段:(此处显示了图像滑块。我想当用户单击一个新片段时打开并显示mTitle)。
public class HomeFragment extends Fragment implements SliderImageAdapter.OnItemClicked {
private SliderImageAdapter sliderImageAdapter;
private List<Banner> bannerList;
bannerList = new ArrayList<>();
sliderImageAdapter = new SliderImageAdapter(getActivity(), bannerList, this);
@Override
public void onItemClick(int position) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container_dashboard, SliderClickFragment.newInstance());
ft.addToBackStack(null);
ft.commit();
}
滑块单击片段XML:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/mSliderTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
滑块单击片段Java :(在此,我想在用户单击图像时显示mTitle)
public class SliderClickFragment extends Fragment {
public static SliderClickFragment newInstance() {
Bundle args = new Bundle();
SliderClickFragment fragment = new SliderClickFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.slider_click_listner,container,false);
return view;
}
}
最佳答案
试试这个。我还没有测试过,但是这种流程应该可以达到您想要实现的目标。
HomeFragment.java
public class HomeFragment extends Fragment implements SliderImageAdapter.OnItemClicked {
private SliderImageAdapter sliderImageAdapter;
private List<Banner> bannerList;
bannerList = new ArrayList<>();
sliderImageAdapter = new SliderImageAdapter(getActivity(), bannerList, this);
@Override
public void onItemClick(int position) {
//GET THE TITLE STRING HERE
String title = bannerList.get(position).getmSliderTitle();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//PASS THIS TITLE TO THE FRAGMENT INSTANCE LIKE THIS
ft.add(R.id.fragment_container_dashboard, SliderClickFragment.newInstance(title));
ft.addToBackStack(null);
ft.commit();
}
SliderClickFragment.java
public class SliderClickFragment extends Fragment {
//EDIT the newInstance Method definition to include title like this:
public static SliderClickFragment newInstance(String title) {
Bundle args = new Bundle();
//PASS THE STRING IN THE BUNDLE WHICH WILL THEN BE FETCHED IN ONCREATEVIEW LATER LIKE THIS
args.putString("title", title);
SliderClickFragment fragment = new SliderClickFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.slider_click_listner,container,false);
//GET THE STRING HERE AND THEN USE IT HOWEVER YOU LIKE
String title = getArguments().getString("title");
return view;
}
}