[FIXED] Pages must fill the whole ViewPager2 (use match_parent) while i set it to match parent

Issue

I use viewPager2 in my app with custom adapter and in layout i use my viewpager2 in linearLayout.
i set the height and width of my viewpager2 to matchParent, and i also set it to matchParent in java code, and set attachToRoot to false, but i still get this error when i lunch my app.
how can i solve this problem?

this is my layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.7"
        android:clipToPadding="false"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

this is my viewpager2 code:

mAdapter = new adapter_vp2_home(getActivity(), ls);

    vp_home.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    vp_home.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

    vp_home.setAdapter(mAdapter);

and this is the log:

java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
        at androidx.viewpager2.widget.ViewPager2$4.onChildViewAttachedToWindow(ViewPager2.java:270)
        at androidx.recyclerview.widget.RecyclerView.dispatchChildAttached(RecyclerView.java:7713)
        at androidx.recyclerview.widget.RecyclerView$5.addView(RecyclerView.java:857)
        at androidx.recyclerview.widget.ChildHelper.addView(ChildHelper.java:107)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:8753)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8711)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8699)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1645)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4141)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3853)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4410)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:526)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:945)
        at android.view.View.layout(View.java:20857)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2961)
E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2648)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1785)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7832)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Solution

i just solve this problem by match parent the custom layout of my custom viewpager2

Answered By – Pooya Jannati Poor

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published