Home > Mobile >  ViewPager2 not working with RecyclerView.Adapter
ViewPager2 not working with RecyclerView.Adapter

Time:03-06

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==&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??????????

CodePudding user response:

change return type in ImagesViewerFragment.java to

return fragmentImagesViewerBinding.getRoot();
  • Related