java页面必须填充整个viewpager2(使用match\u parent),而我将其设置为match parent

hec6srdp  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(941)

我用 viewPager2 在我的应用程序中 custom adapter 在布局上我用我的 viewpager2linearLayout . 我设置了 height 以及 width 我的 viewpager2matchParent ,我还将其设置为 matchParent 在java代码中,并设置 attachToRootfalse ,但当我使用应用程序时仍会出现此错误。我怎样才能解决这个问题?
这是我的布局:

<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>

这是我的viewpager2代码:

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);

这是日志:

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)
pxy2qtax

pxy2qtax1#

设置视图的layoutparams:

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

不是vp_home.setlayoutparams,只是删除它

63lcw9qa

63lcw9qa2#

我只是通过匹配自定义viewpager2的自定义布局来解决这个问题

相关问题