【问题标题】:RecycleView how to hide a reveal a top view on scroll (not the toolbar)RecycleView 如何在滚动时隐藏显示顶视图(不是工具栏)
【发布时间】:2026-01-31 00:00:01
【问题描述】:

我试图(受https://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling(part2) 启发)在向上滚动时隐藏我在 RecycleView 顶部的视图,并在向下滚动时显示它。

但它似乎不太好用。当我向上滚动时,TextView 确实消失了,但它从顶部到列表视图的开头留下了一个空白区域。 (在下面的情况下,我在 textView 上使用 .setTransalteX(dy)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我应该使用什么样的布局来归档相同的内容,而不是在我向上滚动时留下空白区域,列表会占用 textview 的空间?

(执行此操作的应用示例是 Booking.com 的结果列表) (向下滚动时它不会重新出现,但这很好..当列表向上滚动时搜索过滤器向上滚动的事实)

【问题讨论】:

    标签: android view scroll hide android-recyclerview


    【解决方案1】:
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:paddingTop="44dp"
            android:cliptoPadding="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
           xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/test"
            android:layout_width="fill_parent"
            android:layout_height="44dp" />
    </LinearLayout>
    

    【讨论】:

    • android:paddingTop="44dp" 这不是真正的动态.. 我已经尝试过了。 (通过根据文本视图的大小在代码中设置填充)但它的效果与我有线性布局时的效果相同(填充大小的空白)
    • 你想达到什么目的?
    • 如果你只是想让 textview 用 Recyclerview 滚动,只需将 textview 添加为 Recyclerview 的第一个子项。或者搜索 headerRecyclerView 以获得简单的方法。
    • 所以使用一个headerRecyclerview就足够了。所以你说你试试我的框架布局,你添加了 android:cliptoPadding="false" 吗?请使用固定的 44dp 作为高度,然后再试一次。
    • ClipToPadding 表示scrollableview可以在它们的padding中滚动。在RecyclerView中添加TextView更好。