EditText 在 NestedScrollView 中无法滚动

4

布局:我有一个EditText和2个RecyclerView放在一个NestedScrollView中,它们的可见性设置为gone,即不可见。

<androidx.coordinatorlayout.widget.CoordinatorLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    //... toolbar

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="?attr/actionBarSize"
            android:fillViewport="true"
            android:scrollbars="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:backgroundTint="@android:color/transparent"
                    android:gravity="top"
                    android:inputType="textMultiLine|textCapSentences"
                    android:padding="@dimen/activity_margin"
                    android:singleLine="false" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_items"
                    android:padding="@dimen/activity_margin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:scrollbars="vertical"
                    android:visibility="gone" />
   
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_Labels"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="12dp"
                    android:visibility="gone" />

            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
        
    </LinearLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
        android:layout_marginBottom="?actionBarSize" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
            //...
    />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

问题: 当输入的文本超过屏幕高度时,EditText会滚动到光标位置。但是当我尝试向上滚动时,什么也不发生。这是我制作的一个屏幕录像。 无法滚动:
  • 第一次输入/粘贴长文本后。
可以滚动:
  • 重新打开已输入文本的活动后
  • 关闭键盘后
  • 关闭键盘并再次打开它后
搜索类似问题得出:
editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.editText) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
});

那个解决方案不起作用:

  1. 这些问题是关于ScrollViews而不是NestedScrollViews,而NestedScrollView是其中一个提出的解决方案(我已经在使用了)。
  2. 当我添加上述代码时,EditText在键盘显示时可以滚动,但如果没有键盘,则无法滚动 - 尝试滚动会导致选择文本。
  3. 滚动(在键盘打开时)会移动光标。

如果您需要更多信息或者我漏掉了什么,请告知。谢谢!


@HenriqueMS 我移除了包裹 NestedScrollView 的 LinearLayout,现在行为相同。 - Akres
@HenriqueMS 这意味着edittext将位于LinearLayout中,该LinearLayout嵌套在NestedScrollView中,而NestedScrollView又嵌套在NestedScrollView中...但这并没有起作用。 - Akres
@HenriqueMS 也许你可以发布你的代码吗?我可能误解了你之前的指示。 - Akres
2个回答

3
答案其实比我想象的简单。在粘贴您的 XML(并进行必要的更改以使其构建 - 缺少 dimens 等...)之后,我只需将您的 EditText 的高度更改为 wrap_content,问题就解决了。
答案在这里: 比较不同时刻 EditText 的测量值,左侧为 height=match_parent,右侧为 height=wrap_content 左侧: EditText 为空时在屏幕上绘制出一定大小,您粘贴文本后其大小不会改变。 显示/隐藏键盘 是屏幕生命周期中的一个重要事件,称为 配置更改,这会导致元素再次测量和重新绘制。
右侧: 如果将 EditText 的高度更改为 wrap_content,则会在插入后立即强制进行测量和重新绘制。

Measurement of EditText at different times

希望这有所帮助 :)

1
我原以为将wrap_content设置给EditText会使得可点击区域非常小,但实际上并不是这样。这个解决方案很有效,谢谢! :) - Akres
追加问题:你用什么工具获取这些测量数据的? - Akres
没问题 ;) 你可以用几种方式来做,最简单的是在调试器运行应用程序时使用布局检查器(我想是在工具菜单中) https://developer.android.com/studio/debug/layout-inspector - HenriqueMS

-1

将活动设置为:

android:windowSoftInputMode="adjustResize"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接