滚动 header 当滚动 recyclerview 项目时
scroll header when scroll recyclerview items
我有一个带有图像的 header 和一个带有一些项目的 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 时向上滚动 header (RelativeLayout) .
关于如何使用 RecyclerView 滚动 header 有什么想法吗?
将您的根布局设为协调器布局,然后将 RecyclerView Direct 设为协调器布局的子级。将可滚动内容包装在 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>
我有一个带有图像的 header 和一个带有一些项目的 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 时向上滚动 header (RelativeLayout) .
关于如何使用 RecyclerView 滚动 header 有什么想法吗?
将您的根布局设为协调器布局,然后将 RecyclerView Direct 设为协调器布局的子级。将可滚动内容包装在 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>