Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pages must fill the whole ViewPager2 (use match_parent)

I have an item layout where I display an image, product name, and product image. I must display image in 1:1.5 ration using constraint layout. But when I load a small image, below texts not displaying.

Below is my code of item XML:-

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools">      <androidx.constraintlayout.widget.ConstraintLayout         android:id="@+id/coordinatorLayoutCartRoot"         android:layout_width="match_parent"         android:layout_height="match_parent">          <com.jackandphantom.circularimageview.RoundedImage             android:id="@+id/imageViewSlider"             android:layout_width="match_parent"             android:layout_height="0dp"             android:scaleType="centerCrop"             app:layout_constraintBottom_toTopOf="@id/tvTitle"             app:layout_constraintDimensionRatio="WH,1:1.4"             app:layout_constraintEnd_toEndOf="parent"             app:layout_constraintStart_toStartOf="parent"             app:layout_constraintTop_toTopOf="parent"             app:rounded_radius="0"             tools:src="@tools:sample/avatars" />          <TextView             android:id="@+id/tvTitle"             android:layout_width="0dp"             android:layout_height="wrap_content"             android:gravity="center"             android:padding="4dp"             android:text="Fitted crew neck sweater"             android:textColor="@color/colorBlack"             app:layout_constraintEnd_toEndOf="parent"             app:layout_constraintStart_toStartOf="parent"             app:layout_constraintTop_toBottomOf="@id/imageViewSlider" />          <TextView             android:id="@+id/tvPrice"             android:layout_width="0dp"             android:layout_height="wrap_content"             android:gravity="center"             android:padding="4dp"             android:text="$34.00"             android:textColor="@color/colorBlack"             android:textStyle="bold"             app:layout_constraintEnd_toEndOf="parent"             app:layout_constraintStart_toStartOf="parent"             app:layout_constraintTop_toBottomOf="@id/tvTitle" />      </androidx.constraintlayout.widget.ConstraintLayout> </layout> 
  1. Output with long image:- https://i.imgur.com/QVnljX6.png
  2. Output with small image:- https://i.imgur.com/0ZwkVwE.png

And if I replace match_parent with wrap_content, app crashes with below error :-

java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)

like image 357
Mahesh Babariya Avatar asked Oct 11 '19 12:10

Mahesh Babariya


2 Answers

I have found out that the problem is in inflating view holder.

I had the same issue and solved it changing

ViewBinding.inflate(inflater) 

to

ViewBinding.inflate(inflater, parent, false) 
like image 68
Bresiu Avatar answered Sep 17 '22 12:09

Bresiu


While struggling with this problem, I figured it out what's the problem was. My use case was I was using androidx.viewpager2.widget.ViewPager2, and calling normal Views in RecyclerView.

If you notice the error carefully you would see something like this:

 java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)     at androidx.viewpager2.widget.ViewPager2$2.onChildViewAttachedToWindow(ViewPager2.java:170) 

Second line is the key to main issue. If you open ViewPager2.java you would see

 private RecyclerView.OnChildAttachStateChangeListener enforceChildFillListener() {     return new RecyclerView.OnChildAttachStateChangeListener() {         @Override         public void onChildViewAttachedToWindow(@NonNull View view) {             RecyclerView.LayoutParams layoutParams =                     (RecyclerView.LayoutParams) view.getLayoutParams();             if (layoutParams.width != LayoutParams.MATCH_PARENT                     || layoutParams.height != LayoutParams.MATCH_PARENT) {                 throw new IllegalStateException(                         "Pages must fill the whole ViewPager2 (use match_parent)");             }         }          @Override         public void onChildViewDetachedFromWindow(@NonNull View view) {             // nothing         }     }; } 

Android is not taking the match_parent assigned in xml to attach views. May be future improvements would be done in next release of ViewPager2.

Anyways, for now to fix it, set layout params as MATCH_PARENT explicity.

 view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 

This view is the parent view holding View Pager childs.

In you case it would be androidx.constraintlayout.widget.ConstraintLayout.

 view.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
like image 26
androidStud Avatar answered Sep 17 '22 12:09

androidStud