繁体   English   中英

EditText 在 NestedScrollView 内不可滚动

[英]EditText not scrollable inside NestedScrollView

布局:我在 NestedScrollView 中有一个 EditText 和 2 个 RecyclerViews,它们不可见(visibility=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. 滚动(打开键盘)移动光标。

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

答案实际上比我想象的要简单。 粘贴您的 xml(并对其进行必要的更改以构建它 - 缺少尺寸等...)后,我只是将EditText的高度更改为wrap_content并且错误消失了。

答案就在这里:比较EditText在不同时间点的测量值,左侧为height=match_parent ,右侧为height=wrap_content

左侧: EditText以特定大小绘制在空白屏幕上,您粘贴文本并且其大小不会改变。 显示/隐藏键盘是屏幕生命周期中的一个重要事件,它被称为配置更改,这会导致再次测量和重新绘制元素。

右侧:如果将EditText的高度更改为wrap_content ,它将强制测量并在插入后立即重新绘制。

不同时间的 EditText 测量

希望这可以帮助 :)

将活动设置为,

android:windowSoftInputMode="adjustResize"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM