android - 顶部有背景图片的 RecyclerView

标签 android android-layout android-recyclerview android-adapter

我想要实现的是:

https://i.imgur.com/wGgil9B.png

如您所见,在 RecyclerView 中的第一张卡片后方有一个背景图像。

当用户滚动时,背景图像以及“发现”文本和图标也应滚出屏幕。

我研究过将 RecyclerView 放入 NestedScrollView,但是 from what I've read ,这会破坏 RecyclerView 并且不允许它回收 View (我可能是错的)。

我怎样才能实现这样的目标?现在,我的布局非常简单:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

    </android.support.v4.widget.SwipeRefreshLayout>

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

谢谢。

最佳答案

Background image that appears slightly behind the first card in the RecyclerView.

您可以使用 CollapsingToolbarLayout 来做到这一点,它会隐藏背景背后的视差。注意:使用 app:behavior_overlapTop 使 RecyclerView loverlap 成为背景。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            app:titleEnabled="false"
            app:expandedTitleMarginBottom="94dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?colorPrimary"
            >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:src="@drawable/london_flat"
                android:tint="#BB3E50B4"
                android:scaleType="centerCrop"
                />

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@null"
                app:layout_collapseMode="pin"
                style="@style/ToolBarWithNavigationBack"
                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        app:behavior_overlapTop="78dp"
        android:layout_margin="8dp"
        android:background="@color/cardview_light_background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

    </android.support.v7.widget.RecyclerView>

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

结果:

enter image description here

关于android - 顶部有背景图片的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44479301/

相关文章:

java - 如何在运行时动态生成ContextMenu项目?

Android 自定义键盘,无法实例化以下类 :

android - RecyclerView 未显示(正在显示)

android - RecyclerView 在项目之间创建边距

Android:为什么在NestedScrollView 内部的RecyclerView 底部有一个空白区域?

Android:访问构成视频的图像

Android:不显示进度对话框

c# - Xamarin Forms Android DatePicker - 只希望显示微调器,但日历也是如此

android - 加权按钮上的文本不占用与按钮相同的空间

android - 键盘打开时,带有 fragment 的 Activity 不会调整大小