英文:
RecyclerView Items loosing MATCH_PARENT setting after clicking on EditText Field
问题
以下是翻译的内容:
你好,我遇到了一个非常奇怪的问题,让我疯狂不已。我有一个 DialogFragment,在其中放置了一个 RecyclerView。我为 RecyclerView 创建了一个自定义适配器,并为 DialogFragment 和 RecyclerView 适配器分别创建了自定义的 XML 布局。
当我打开对话框时,视图表现得很好,就像应该的一样。
[![对话框打开时的正常行为][1]][1]
问题出现在当我点击 EditText 字段(图片中的黄色框)并且弹出屏幕键盘时。然后由于某种原因,整个布局混乱了,表现出我定义的 WRAP_CONTENT 的行为。
[![点击文本编辑字段后出现的问题行为][2]][2]
我不知道为什么会发生这种情况。我的代码看起来没问题,在查看了其他类似问题后,代码在我看来是正确的。
这是 RecyclerView 的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 其他视图元素... -->
<EditText
android:id="@+id/newWeightInput"
android:layout_width="69dp"
android:layout_height="36dp"
android:layout_marginEnd="2dp"
android:background="@drawable/border_qty"
android:ems="10"
android:inputType="number"
android:maxLength="4"
android:padding="4dp"
android:textAlignment="center"
android:textColor="@color/colorSecondaryVariant"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/kgIcon"
app:layout_constraintTop_toTopOf="parent"
tools:text="9999" />
<!-- 其他视图元素... -->
</androidx.constraintlayout.widget.ConstraintLayout>
这是适配器中的 onCreateViewHolder 方法中的代码:
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rvr_package, parent, false);
return new PackagesViewHolder(view);
}
这是 DialogFragment 的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 其他视图元素... -->
<Button
android:id="@+id/updateWeightBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="8dp"
android:backgroundTint="@color/colorAccent"
android:text="@string/update"
app:layout_constraintBottom_toBottomOf="@+id/closeBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/closeBtn" />
<!-- 其他视图元素... -->
</androidx.constraintlayout.widget.ConstraintLayout>
注意:由于您要求只返回翻译好的部分,我已经删除了一些不相关的内容。如果您需要更多信息,请随时提问。
英文:
Hello I have a very weird problem that it drives me crazy. I have a DialogFragment and inside that a RecyclerView. I have a custom adapter for my RecyclerView and a custom XML layout for both the DialogFragment and the RecyclerView Adpater.
When I open the dialog the view is just fine. As it should be.
The problem appears when I click inside the EditText Field (the yellow box in picture) and the on screen keyboard pops up. Then for some reason the entire layout messes and it behaves as I defined it as WRAP_CONTENT.
I have no idea why is that happening. My code seems right and after looking up on other similar questions the code is right to my eyes.
This is the RecyclerView custom layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/packitemnoField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_barcode_small"
android:drawablePadding="4dp"
android:paddingStart="4dp"
android:paddingTop="2dp"
android:paddingEnd="4dp"
android:paddingBottom="2dp"
android:textAlignment="viewStart"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="0062020512845" />
<TextView
android:id="@+id/qtyDescTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:text="@string/tweightLabel"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/packDescField" />
<TextView
android:id="@+id/RemainsDescTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
android:text="@string/weightLabel"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/qtyDescTV"
app:layout_constraintTop_toBottomOf="@+id/qtyDescTV" />
<TextView
android:id="@+id/thWeightField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:textColor="@color/darkGrey"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/qtyDescTV"
app:layout_constraintTop_toTopOf="@+id/qtyDescTV"
tools:text="9999 kg." />
<TextView
android:id="@+id/weightField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:textColor="@color/successGreen"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/qtyDescTV"
app:layout_constraintTop_toTopOf="@+id/RemainsDescTV"
tools:text="9999 kg." />
<TextView
android:id="@+id/packDescField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:textColor="@color/od_color1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/packitemnoField"
tools:text="ALBIO / ΠΡΟΦΙΛ ΠΕΛΑΤΗ" />
<ImageView
android:id="@+id/kgIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginEnd="4dp"
android:tint="@color/darkGrey"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/newWeightInput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/newWeightInput"
app:srcCompat="@drawable/ic_weight" />
<EditText
android:id="@+id/newWeightInput"
android:layout_width="69dp"
android:layout_height="36dp"
android:layout_marginEnd="2dp"
android:background="@drawable/border_qty"
android:ems="10"
android:inputType="number"
android:maxLength="4"
android:padding="4dp"
android:textAlignment="center"
android:textColor="@color/colorSecondaryVariant"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/kgIcon"
app:layout_constraintTop_toTopOf="parent"
tools:text="9999" />
<TextView
android:id="@+id/packDateField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:text="26/03/2020"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/newWeightLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:text="@string/newWeight"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/newWeightInput"
app:layout_constraintEnd_toStartOf="@+id/newWeightInput"
app:layout_constraintTop_toTopOf="@+id/newWeightInput" />
</androidx.constraintlayout.widget.ConstraintLayout>
And this is the code in the onCreateViewHolder method in the adapter
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rvr_package, parent, false);
return new PackagesViewHolder(view);
}
Here is tha DialogFragment layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/updateWeightBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="8dp"
android:backgroundTint="@color/colorAccent"
android:text="@string/update"
app:layout_constraintBottom_toBottomOf="@+id/closeBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/closeBtn" />
<Button
android:id="@+id/closeBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:text="@string/close"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/packagesRecyclerView" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/packagesRecyclerView"
android:layout_width="0dp"
android:layout_height="400dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案1
得分: 1
你可以尝试将以下行添加到你的 DialogFragment 的 constraintLayout 中:
android:fitsSystemWindows="true"
英文:
you can try add to your constraintLayout of DialogFragment this line :
android:fitsSystemWindows="true"
答案2
得分: 0
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/packagesRecyclerView"
android:layout_width="match_parent"
android:layout_height="400dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
英文:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/packagesRecyclerView"
android:layout_width="match_parent"
android:layout_height="400dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论