无法在NestedScrollView

时间:2019-05-31 16:07:16

标签: java android android-layout android-edittext

布局:我在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.

无法滚动:

  • 第一次输入/粘贴长文本后。

可以滚动:

  • 使用已输入的文字重新打开活动后
  • 关闭键盘后
  • 关闭键盘并再次打开后

搜索相似的问题会产生

...

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. 滚动(在打开键盘的情况下)移动光标。

如果您需要更多信息,或者我错过了任何事情,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:1)

答案实际上比我想的要简单。 粘贴了xml(并对其进行了必要的更改以使其构建-丢失了变暗的阴影等。)之后,我只是将EditText的高度更改为wrap_content,该错误消失了。

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

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

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

Measurement of EditText at different times

希望这会有所帮助:)

答案 1 :(得分:0)

将活动设置为

android:windowSoftInputMode="adjustResize"