布局:我有一个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会滚动到光标位置。但是当我尝试向上滚动时,什么也不发生。这是我制作的一个屏幕录像。 无法滚动:
- 第一次输入/粘贴长文本后。
- 重新打开已输入文本的活动后
- 关闭键盘后
- 关闭键盘并再次打开它后
- ScrollView内EditText不可滚动
- 在ScrollView和ViewFlipper中启用可滚动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;
}
});
那个解决方案不起作用:
- 这些问题是关于ScrollViews而不是NestedScrollViews,而NestedScrollView是其中一个提出的解决方案(我已经在使用了)。
- 当我添加上述代码时,EditText在键盘显示时可以滚动,但如果没有键盘,则无法滚动 - 尝试滚动会导致选择文本。
- 滚动(在键盘打开时)会移动光标。
如果您需要更多信息或者我漏掉了什么,请告知。谢谢!