Edittext 不断滚动到它在 RecyclerView 中的位置

问题描述

先决条件

ma​​in_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.nestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbarFadeDuration="2"
        android:scrollbars="vertical|horizontal">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.core.widget.nestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

recyclerview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <...>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
</LinearLayout>

another_recyclerview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/divider_default_margin"
    android:orientation="vertical"
    android:showDividers="middle">

    <...>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/anotherRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <...>

</LinearLayout>

edittext_item_layout.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/formTextClearButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>

    <...>

</androidx.constraintlayout.widget.ConstraintLayout>

问题描述

我在运行时将 recyclerview_layout.xml 添加container 内的 main_layout.xmlrecyclerview_layout.xml 有时包含 another_recyclerview_layout.xml,它有一个 RecyclerView,该 edittext_item_layout.xml 具有多种视图类型,其中一种由 another_recyclerview_layout.xml 表示。当后者存在于 recyclerview_layout.xml 并且 TextInputEditText 填充了一些其他项目时,整个布局具有奇怪的滚动行为,当用户点击 TextInputEditText 之一时。这就像它试图捕捉到焦点 android:focusable="true" 并且用户无法再从该视图滚动离开。 (见下面的gif)

Scrolling bug

已经尝试过的解决方

  1. android:focusableInTouchMode="true"nestedScrollView 设置为 recyclerview_layout.xml 的直接子级,正如您在 android:descendantFocusability="blocksDescendants" 中已经看到的那样。还尝试将这些属性添加到所有其他容器中(哭)
  2. anotherRecyclerView 设置为 TextInputEditText 的父级。此解决方案对我不起作用,因为我需要 SoftKeyboard 才能完全正常运行。
  3. 收听 2.0.4 事件并在键盘隐藏后立即清除焦点。此解决方案引发了另外两个问题:
    • 失去滚动位置
    • 布局仍然有问题,如果用户键盘可见时尝试滚动

图书馆

约束布局:1.2.0

RecyclerView:1.2.1

材料:"AllData.data": 1,"AllData.data.Brand": { $ifNull: [ "$AllData.data.Brand","$AllData.name" ] }

帮助!

我的想法不多了,非常感谢任何帮助。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)