【问题标题】:RecyclerView with background image at top顶部有背景图片的 RecyclerView
【发布时间】:2017-06-11 00:24:39
【问题描述】:

我想要实现的是:

如您所见,在 RecyclerView 的第一张卡片后面稍稍显示了一个背景图像。

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

我已经考虑将 RecyclerView 放入 NestedScrollView,但 from what I've read,这会破坏 RecyclerView 并且不允许它回收视图(我可能是错的)。

我将如何实现这样的目标?现在,我的布局非常简单:

<?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>

谢谢。

【问题讨论】:

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


    【解决方案1】:

    显示在第一张卡片后面的背景图片 回收站视图。

    您可以使用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>
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2015-04-26
      • 1970-01-01
      相关资源
      最近更新 更多