【问题标题】:NestedScrollView scrolls on top when content changes内容更改时,NestedScrollView 在顶部滚动
【发布时间】:2021-10-20 05:43:48
【问题描述】:

我有一个fragment_layout.xml,带有两个按钮(filter_1_btnfilter_2_btn),它们对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>


【问题讨论】:

  • 能贴一下过滤的代码吗,过滤后的数据怎么设置?

标签: android android-layout android-recyclerview android-nestedscrollview


【解决方案1】:

这是因为 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>

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多