java - EditText 在 NestedScrollView 内不可滚动

标签 java android android-layout android-edittext android-nestedscrollview

版面:我在 NestedScrollView 中有一个 EditText 和 2 个 RecyclerView,它们不可见(可见性 = 消失)

<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 会向下滚动到光标所在的位置。但是当我尝试向上滚动时,什么也没有发生。 Here's a screen recording I made.
无法滚动:
  • 第一次输入/粘贴长文本后。

  • 可以滚动:
  • 重新打开已输入文本的 Activity 后
  • 关闭键盘后
  • 关闭键盘并再次打开后

  • 搜索类似问题得到:
  • EditText not scrollable inside ScrollView
  • Enable Scrollable EditText within a ScrollView and ViewFlipper
  • ...和其他具有相同答案的结果:

  • ...
    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;
        }
    });
    
    该解决方案不起作用:
  • 这些问题是关于 ScrollViews,而不是 NestedScrollViews。 NestedScrollView 是建议的解决方案之一(我已经在使用)
  • 当我添加上面的代码时, EditText 有点可滚动,但只有在显示键盘时。如果不是,则无法滚动 - 尝试滚动会导致选择文本。
  • 滚动(打开键盘)移动光标。

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

    最佳答案

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

    答案就在这里:
    比较 EditText 的测量值在不同的时间点,左边是 height=match_parent右边是 height=wrap_content
    在左边:EditText以一定大小在屏幕上绘制,您粘贴文本并且它的大小不会改变。 显示/隐藏键盘 是屏幕生命中的一个重要事件,它被称为 配置变更这会导致元素被再次测量并重新绘制。

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

    Measurement of EditText at different times

    希望这可以帮助 :)

    关于java - EditText 在 NestedScrollView 内不可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398459/

    相关文章:

    android - adb reboot 挂起 Genymotion

    Android 发布版本 - MissingDefaultResource "error"

    java - 基于 Android 模拟器中特定定位器的最佳向上/向下滚动方法是什么?

    android - 无法从 xml 布局中引用 Observable 类字段

    android - 只有在 GridLayout 中显示 ViewGroup 的第一个 subview - 后续单元格在视觉上是无 subview 的

    java.io.BufferedReader.readLine() 是非阻塞的

    java - 无法从另一个类创建类对象

    java - 尝试从图片中读取元数据时获取 FileNotFoundException(权限被拒绝)

    java - 每当我运行 android 登录应用程序时,它都会在日志中崩溃一系列错误消息

    android - 向 Snackbar View 添加边距