RecyclerView的项目在单击EditText字段后丧失了MATCH_PARENT设置。

huangapple go评论74阅读模式
英文:

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.

RecyclerView的项目在单击EditText字段后丧失了MATCH_PARENT设置。

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.

RecyclerView的项目在单击EditText字段后丧失了MATCH_PARENT设置。

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/packitemnoField&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:drawableStart=&quot;@drawable/ic_barcode_small&quot;
        android:drawablePadding=&quot;4dp&quot;
        android:paddingStart=&quot;4dp&quot;
        android:paddingTop=&quot;2dp&quot;
        android:paddingEnd=&quot;4dp&quot;
        android:paddingBottom=&quot;2dp&quot;
        android:textAlignment=&quot;viewStart&quot;
        android:textColor=&quot;#000000&quot;
        android:textSize=&quot;18sp&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        tools:text=&quot;0062020512845&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/qtyDescTV&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;4dp&quot;
        android:layout_marginTop=&quot;4dp&quot;
        android:text=&quot;@string/tweightLabel&quot;
        android:textSize=&quot;14sp&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/packDescField&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/RemainsDescTV&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;2dp&quot;
        android:layout_marginBottom=&quot;4dp&quot;
        android:text=&quot;@string/weightLabel&quot;
        android:textSize=&quot;14sp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;@+id/qtyDescTV&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/qtyDescTV&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/thWeightField&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;2dp&quot;
        android:textColor=&quot;@color/darkGrey&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintStart_toEndOf=&quot;@+id/qtyDescTV&quot;
        app:layout_constraintTop_toTopOf=&quot;@+id/qtyDescTV&quot;
        tools:text=&quot;9999 kg.&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/weightField&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;2dp&quot;
        android:textColor=&quot;@color/successGreen&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintStart_toEndOf=&quot;@+id/qtyDescTV&quot;
        app:layout_constraintTop_toTopOf=&quot;@+id/RemainsDescTV&quot;
        tools:text=&quot;9999 kg.&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/packDescField&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;4dp&quot;
        android:textColor=&quot;@color/od_color1&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/packitemnoField&quot;
        tools:text=&quot;ALBIO / ΠΡΟΦΙΛ ΠΕΛΑΤΗ&quot; /&gt;

    &lt;ImageView
        android:id=&quot;@+id/kgIcon&quot;
        android:layout_width=&quot;20dp&quot;
        android:layout_height=&quot;20dp&quot;
        android:layout_marginEnd=&quot;4dp&quot;
        android:tint=&quot;@color/darkGrey&quot;
        android:visibility=&quot;visible&quot;
        app:layout_constraintBottom_toBottomOf=&quot;@+id/newWeightInput&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;@+id/newWeightInput&quot;
        app:srcCompat=&quot;@drawable/ic_weight&quot; /&gt;

    &lt;EditText
        android:id=&quot;@+id/newWeightInput&quot;
        android:layout_width=&quot;69dp&quot;
        android:layout_height=&quot;36dp&quot;
        android:layout_marginEnd=&quot;2dp&quot;
        android:background=&quot;@drawable/border_qty&quot;
        android:ems=&quot;10&quot;
        android:inputType=&quot;number&quot;
        android:maxLength=&quot;4&quot;
        android:padding=&quot;4dp&quot;
        android:textAlignment=&quot;center&quot;
        android:textColor=&quot;@color/colorSecondaryVariant&quot;
        android:textSize=&quot;20sp&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toStartOf=&quot;@+id/kgIcon&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        tools:text=&quot;9999&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/packDateField&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;4dp&quot;
        android:layout_marginEnd=&quot;4dp&quot;
        android:text=&quot;26/03/2020&quot;
        android:textSize=&quot;12sp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/newWeightLbl&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;2dp&quot;
        android:text=&quot;@string/newWeight&quot;
        android:textSize=&quot;12sp&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintBottom_toBottomOf=&quot;@+id/newWeightInput&quot;
        app:layout_constraintEnd_toStartOf=&quot;@+id/newWeightInput&quot;
        app:layout_constraintTop_toTopOf=&quot;@+id/newWeightInput&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;

    &lt;androidx.constraintlayout.widget.Guideline
        android:id=&quot;@+id/guideline&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:orientation=&quot;vertical&quot;
        app:layout_constraintGuide_percent=&quot;0.5&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/updateWeightBtn&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;4dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:backgroundTint=&quot;@color/colorAccent&quot;
        android:text=&quot;@string/update&quot;
        app:layout_constraintBottom_toBottomOf=&quot;@+id/closeBtn&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;@+id/guideline&quot;
        app:layout_constraintTop_toTopOf=&quot;@+id/closeBtn&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/closeBtn&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginTop=&quot;8dp&quot;
        android:layout_marginEnd=&quot;4dp&quot;
        android:text=&quot;@string/close&quot;
        app:layout_constraintEnd_toStartOf=&quot;@+id/guideline&quot;
        app:layout_constraintHorizontal_bias=&quot;0.0&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/packagesRecyclerView&quot; /&gt;

    &lt;ProgressBar
        android:id=&quot;@+id/progressBar&quot;
        style=&quot;?android:attr/progressBarStyle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:visibility=&quot;gone&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/packagesRecyclerView&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;400dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

你可以尝试将以下行添加到你的 DialogFragment 的 constraintLayout 中:

android:fitsSystemWindows="true"
英文:

you can try add to your constraintLayout of DialogFragment this line :

android:fitsSystemWindows=&quot;true&quot;

答案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" />
英文:
&lt;androidx.recyclerview.widget.RecyclerView
    android:id=&quot;@+id/packagesRecyclerView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;400dp&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

huangapple
  • 本文由 发表于 2020年4月3日 20:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61012418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定