当滚动RecyclerView项目时,如何滚动标题栏

5

我有一个带有图片的标题和一个包含一些项目的RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/header"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#ffffff">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="0dp"
            android:id="@+id/imageView"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

    </RelativeLayout>


    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:scrollbars="vertical"
        android:layout_alignParentLeft="true" />


</LinearLayout>

RelativeLayout(相对布局)固定在RecyclerView(可滚动列表)的顶部。 我的目标是当滚动RecyclerView时,将此标题(RelativeLayout)向上滚动。


有什么想法可以使标题随着RecyclerView一起滚动吗?

1个回答

13

将您的根布局设置为协调布局,然后将 RecyclerView 直接放置在协调布局中。将可滚动内容包装在 AppBarLayout 中,然后应用滚动行为。尝试以下代码,这应该会帮助您。

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.design.widget.AppBarLayout

        android:layout_height="wrap_content"
        android:layout_width="match_parent"
    >

    <RelativeLayout
        android:id="@+id/header"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_scrollFlags="scroll"
        android:background="#ffffff">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="0dp"
            android:id="@+id/imageView"
            android:src="@mipmap/ic_launcher"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

    </RelativeLayout>



</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:scrollbars="vertical"
    android:layout_alignParentLeft="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />



 </android.support.design.widget.CoordinatorLayout>

谢谢回答。我有工具栏,然后是固定的标题内容,最后是RecyclerView。我想滚动固定内容和RecyclerView,但不想滚动工具栏。我该怎么做? - Chirag Savsani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接