ViewPager2 无法与 RecyclerView.Adapter 一起使用
ImagesViewerAdapter.java
public class ImagesViewerAdapter extends RecyclerView.Adapter<ImagesViewerAdapter.VH> {
private final Context context;
private final String[] images;
public ImagesViewerAdapter(Context context, String[] images) {
this.context = context;
this.images = images;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VH(RowAdImageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
Glide.with(context).load(images[position]).into(holder.rowAdImageBinding.rowAdImageImageViewImage);
}
@Override
public int getItemCount() {
return images.length;
}
public static class VH extends RecyclerView.ViewHolder {
RowAdImageBinding rowAdImageBinding;
public VH(@NonNull RowAdImageBinding rowAdImageBinding) {
super(rowAdImageBinding.getRoot());
this.rowAdImageBinding = rowAdImageBinding;
}
}
}
ImagesViewerFragment.java
public class ImagesViewerFragment extends Fragment {
String[] images = {"https://images.unsplash.com/photo-1612012460576-5d51b5b04b00?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8d2FsbHBhcGVyJTIwZm9yJTIwbW9iaWxlfGVufDB8fDB8fA%3D%3D&w=1000&q=80"};
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentImagesViewerBinding fragmentImagesViewerBinding = FragmentImagesViewerBinding.inflate(inflater, container, false);
fragmentImagesViewerBinding.fragmentImagesViewerViewPager2Container.setAdapter(new ImagesViewerAdapter(requireContext(), images));
return inflater.inflate(R.layout.fragment_images_viewer, container, false);
}
}
fragment_images_viewer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager2.widget.ViewPager2
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_images_viewer___view_pager_2___container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImagesViewerFragment" />
row_ad_image.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/row_ad_image___image_view___image"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ContentDescription" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main___fragment___container"
android:name="com.app.xta.ImagesViewerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
我试图显示 ViewPager 内的图像,但它没有显示任何图像,而且 Adapter 内的 onCreateViewHolder 和 onBindViewHolder 也不会被调用。
问题是什么??????????
ImagesViewerAdapter.java
public class ImagesViewerAdapter extends RecyclerView.Adapter<ImagesViewerAdapter.VH> {
private final Context context;
private final String[] images;
public ImagesViewerAdapter(Context context, String[] images) {
this.context = context;
this.images = images;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VH(RowAdImageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
Glide.with(context).load(images[position]).into(holder.rowAdImageBinding.rowAdImageImageViewImage);
}
@Override
public int getItemCount() {
return images.length;
}
public static class VH extends RecyclerView.ViewHolder {
RowAdImageBinding rowAdImageBinding;
public VH(@NonNull RowAdImageBinding rowAdImageBinding) {
super(rowAdImageBinding.getRoot());
this.rowAdImageBinding = rowAdImageBinding;
}
}
}
ImagesViewerFragment.java
public class ImagesViewerFragment extends Fragment {
String[] images = {"https://images.unsplash.com/photo-1612012460576-5d51b5b04b00?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8d2FsbHBhcGVyJTIwZm9yJTIwbW9iaWxlfGVufDB8fDB8fA%3D%3D&w=1000&q=80"};
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentImagesViewerBinding fragmentImagesViewerBinding = FragmentImagesViewerBinding.inflate(inflater, container, false);
fragmentImagesViewerBinding.fragmentImagesViewerViewPager2Container.setAdapter(new ImagesViewerAdapter(requireContext(), images));
return inflater.inflate(R.layout.fragment_images_viewer, container, false);
}
}
fragment_images_viewer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager2.widget.ViewPager2
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_images_viewer___view_pager_2___container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImagesViewerFragment" />
row_ad_image.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/row_ad_image___image_view___image"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ContentDescription" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main___fragment___container"
android:name="com.app.xta.ImagesViewerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
I'm trying to show the images inside the ViewPager but it's not showing any image, Also onCreateViewHolder and onBindViewHolder inside Adapter are not called never.
What is the problem??????????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 ImagesViewerFragment.java 中的返回类型更改为
change return type in ImagesViewerFragment.java to