Android 页面必须填充整个ViewPage2(使用匹配父项)
我有一个显示图像、产品名称和产品图像的项目布局。我必须使用约束布局以1:1.5的比例显示图像。但当我加载一个小图像时,下面的文本不会显示 下面是我的项目XML代码:-Android 页面必须填充整个ViewPage2(使用匹配父项),android,android-viewpager2,Android,Android Viewpager2,我有一个显示图像、产品名称和产品图像的项目布局。我必须使用约束布局以1:1.5的比例显示图像。但当我加载一个小图像时,下面的文本不会显示 下面是我的项目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-a
<?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>
带小图像的输出:-
如果我将match_parent替换为wrap_内容,应用程序将崩溃,出现以下错误:-
java.lang.IllegalStateException:页面必须填充整个ViewPage2(使用匹配父项)
在努力解决这个问题的过程中,我发现了问题所在。我的用例是使用androidx.viewpager2.widget.viewpager2,并在RecyclerView中调用普通视图
如果您仔细注意到错误,您会看到如下内容:
java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
at androidx.viewpager2.widget.ViewPager2$2.onChildViewAttachedToWindow(ViewPager2.java:170)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = inflater.inflate(R.layout.yourlayout, parent, false)
view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
return ViewHolder(view)
}
inner class ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
}
第二行是主要问题的关键。如果打开ViewPager2.java,您将看到
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没有使用xml中指定的match_父对象来附加视图。未来的改进可能会在ViewPager2的下一个版本中完成
无论如何,现在要修复它,请将布局参数设置为MATCH_PARENT explicity
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
此视图是包含视图寻呼机子视图的父视图
在您的情况下,它将是androidx.constraintlayout.widget.constraintlayout
view.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
我发现问题出在视图支架膨胀上
我也遇到了同样的问题,并且解决了它
ViewBinding.inflate(inflater)
到
对我来说,问题是ViewPager2中的页面(即适配器的父/根布局)不匹配,因此错误为
java.lang.IllegalStateException:页面必须填满整个ViewPage2(使用match\u parent)
我也遇到了同样的问题。
我使用ViewPager2来显示滑块。但其XML项与父项不匹配。
更改后,问题得到解决。viewpager2项的宽度和高度应匹配。但是,如果使用视图绑定,则应放大布局,例如碎片:
ViewBinding.inflate(inflater, parent, false)
我现在面临着同样的问题,您的适配器项的宽度和高度必须匹配\u parent我也面临着同样的问题,解决方案是将高度设置为项目视图的匹配父级,即适配器类中使用的布局必须将高度设置为匹配父级
例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".presentation.home.view.BannerFragment">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager_banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
因此它将占据适配器布局的高度。您应该这样写:
java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
at androidx.viewpager2.widget.ViewPager2$2.onChildViewAttachedToWindow(ViewPager2.java:170)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = inflater.inflate(R.layout.yourlayout, parent, false)
view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
return ViewHolder(view)
}
inner class ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
}
在ViewPager2的适配器项中设置宽度和高度匹配父级适配器的布局宽度和布局高度必须匹配父级设置android:layout\u宽度和android:layout\u高度以匹配ViewPager2和适配器布局的父级如果在约束布局中使用ViewPager2,只需将ViewPager2与相对布局
<RelativeLayout
android:id="@+id/sub_service_list_view_cont"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/basket"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view14">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/sub_service_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
使用将父项
匹配到查看页面2的项目
我也有同样的错误,有时我在ViewPager2
的适配器项中发现问题后,我将其从wrap\u content
更改为match\u parent
,并将其修复
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"/> <!--Change height here to match_parent-->
如果您不在充气机中使用父级,也可能出现这种情况。
此崩溃,但您有以下例外情况:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OnboardViewHolder {
return OnboardViewHolder(parent.context.inflate(
R.layout.view_onboard_item, null, false
))
}
这是有效的:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OnboardViewHolder {
return OnboardViewHolder(parent.context.inflate(
R.layout.view_onboard_item, parent, false
))
}
如果要更改页面高度,只需更改viewpagers的高度。@Mahesh我也有同样的问题。你找到解决办法了吗?@androidStud没有yet@MaheshBabariya请参阅下面我的回答。是的,将其设置为与父项匹配,如果要缩小它,请尝试在视图内或使用视图页面本身进行。这不是答案,viewPagger2始终要求项目布局与父项高度和宽度匹配。修正相反的布局。这为我修正了问题。如果没有对父视图组的正确引用,视图可能无法与父视图匹配。因此,它被解释为wrap_内容并抛出上述错误。使用此视图解决。setLayoutParams(新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级));对我来说,我解决了这个问题,通过使用上面的行在视图寻呼机适配器的onCreateViewHolder中设置参数。这对我来说很有效!我也面临同样的问题。如果我只想使用宽度作为包装内容,该怎么办?将适配器项设置为匹配父项并设置viewpager宽度包装内容我面临相同的问题,但使用的是ViewBinding