ConstraintLayout中嵌套的NestedScrollView无法滚动该如何解决?
问题原因
1. NestedScrollView约束与高度配置错误
你当前给NestedScrollView设置的高度为wrap_content,且仅绑定了顶部和左侧约束,没有绑定底部约束,导致NestedScrollView会无限延伸至父布局底部之外,所有内容直接完全展开,没有固定的滚动视口,自然无法触发滚动。同时无底部约束的NestedScrollView还会和底部固定按钮区域产生重叠。
2. 滚动冲突
RecyclerView默认开启嵌套滚动特性,放在NestedScrollView内部时,滚动事件会被RecyclerView优先消费,导致外层NestedScrollView无法响应滚动操作。
修复步骤
- 给底部装按钮的
LinearLayout添加id,方便后续绑定约束:
<LinearLayout android:id="@+id/bottom_btn_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <!-- 按钮代码不变 --> </LinearLayout>
- 修改
NestedScrollView的高度与约束配置,将高度设为0dp(ConstraintLayout中代表匹配约束),同时绑定右侧和底部约束:
<androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintTop_toBottomOf="@id/layout_2" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@id/bottom_btn_container"> <!-- 内部子布局代码不变 --> </androidx.core.widget.NestedScrollView>
- 给两个
RecyclerView关闭嵌套滚动特性,避免滚动冲突,在xml中添加属性即可:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_pick" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:nestedScrollingEnabled="false" tools:listitem="@layout/inbound_item_detail_sector" tools:visibility="visible" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_buffer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:nestedScrollingEnabled="false" tools:listitem="@layout/inbound_item_detail_sector" tools:visibility="gone" />
可选优化
如果你的RecyclerView列表项数量较多,不建议给RecyclerView设置wrap_content高度,该配置会导致RecyclerView一次性加载所有列表项,失去视图复用能力,列表过长时会出现卡顿。可改为单个RecyclerView多视图类型实现不同区域的展示,滚动性能更好。
内容的提问来源于stack exchange,提问作者Sorry
相关产品推荐
相关产品推荐

