ViewPager2 无法与 RecyclerView.Adapter 一起使用

发布于 2025-01-11 21:47:43 字数 3501 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2025-01-18 21:47:43

将 ImagesViewerFragment.java 中的返回类型更改为

return fragmentImagesViewerBinding.getRoot();

change return type in ImagesViewerFragment.java to

return fragmentImagesViewerBinding.getRoot();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文