android - 内容更改时,NestedScrollView 在顶部滚动
问题描述
我有一个fragment_layout.xml
带有两个按钮(filter_1_btn
, filter_2_btn
)的 a ,它们对 a 的项目执行过滤RecyclerView
。问题是当我滚动一点(因为TextView
上面的按钮包含多行文本)然后应用过滤时,NestedScrollView
屏幕顶部会滚动。过滤后有没有办法保持相同的滚动高度?
片段布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nested_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:id="@+id/description_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Very long random text..." />
<Button
android:id="@+id/filter_1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Filter 1"/>
<Button
android:id="@+id/filter_2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Filter 2"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="24dp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
解决方案
发生这种情况是因为 recyclerView 的容器设置为 wrap_content ,因此当高度小于 NestedScrollView 时,它会滚动到顶部。
您可以通过提供大于 NestedScrollView 的高度来修复它:
所以添加 minHeight :
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nested_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:id="@+id/description_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Very long random text..." />
<Button
android:id="@+id/filter_1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Filter 1" />
<Button
android:id="@+id/filter_2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Filter 2" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="1000dp">
<!-- Add min height to support scrolling-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp" />
</FrameLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>