Mah*_*iya 4 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-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>
Run Code Online (Sandbox Code Playgroud)
而且,如果我用wrap_content替换match_parent,则应用程序崩溃并显示以下错误:-
java.lang.IllegalStateException:页面必须填充整个ViewPager2(使用match_parent)
and*_*tud 19
在努力解决这个问题的同时,我想出了问题所在。我的用例是我使用 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)
Run Code Online (Sandbox Code Playgroud)
第二行是主要问题的关键。如果你打开 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
}
};
}
Run Code Online (Sandbox Code Playgroud)
Android 不会将 xml 中分配的 match_parent 用于附加视图。未来的改进可能会在 ViewPager2 的下一个版本中完成。
无论如何,现在要修复它,将布局参数明确设置为 MATCH_PARENT。
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Run Code Online (Sandbox Code Playgroud)
此视图是包含 View Pager 子项的父视图。
在你的情况下,它将是 androidx.constraintlayout.widget.ConstraintLayout。
view.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Run Code Online (Sandbox Code Playgroud)
小智 12
你应该这样写:
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) {
}
Run Code Online (Sandbox Code Playgroud)
match_parent您item的ViewPager2我有同样的错误,有时我在ViewPager2的适配器项中发现问题后,我将其从 更改wrap_content为match_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-->
Run Code Online (Sandbox Code Playgroud)
viewpager2 项目应该具有 match_parent 宽度和高度。但如果您使用视图绑定,您应该膨胀布局,例如片段:
ViewBinding.inflate(inflater, parent, false)
Run Code Online (Sandbox Code Playgroud)
我发现问题出在视图固定器膨胀中。
我有同样的问题并解决了它的变化
ViewBinding.inflate(inflater)
Run Code Online (Sandbox Code Playgroud)
至
ViewBinding.inflate(inflater, parent, false)
Run Code Online (Sandbox Code Playgroud)
对我来说,问题是 ViewPager2 中的页面(即适配器的父/根布局)不是 match_parent 因此错误是
java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
| 归档时间: |
|
| 查看次数: |
620 次 |
| 最近记录: |