ViewPager2 不适用于 RecyclerView.Adapter

ViewPager2 not working with 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 中的图像,但它没有显示任何图像,而且永远不会调用适配器中的 onCreateViewHolder 和 onBindViewHolder。

问题是什么????????

将 return 中的 ImagesViewerFragment.java 类型更改为

return fragmentImagesViewerBinding.getRoot();