版面:我在 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;
}
});
该解决方案不起作用:如果您需要更多信息或我遗漏了什么,请告诉我。谢谢!
最佳答案
答案实际上比我想象的要简单。
粘贴您的 xml 后(并对其进行必要的更改以构建 - 缺少尺寸等...)我只是更改了您的 EditText
的高度至wrap_content
并且错误消失了。
答案就在这里:
比较 EditText
的测量值在不同的时间点,左边是 height=match_parent
右边是 height=wrap_content
在左边:EditText
以一定大小在屏幕上绘制,您粘贴文本并且它的大小不会改变。 显示/隐藏键盘 是屏幕生命中的一个重要事件,它被称为 配置变更这会导致元素被再次测量并重新绘制。
在右侧:
如果更改 EditText
的高度至wrap_content
它将在插入后立即强制测量并重新绘制。
希望这可以帮助 :)
关于java - EditText 在 NestedScrollView 内不可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398459/