android - 通过滚动回收器 View 折叠标题图像

标签 android android-recyclerview material-design android-collapsingtoolbarlayout

我有一个像这样的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="realup.ir.teacher.MainActivity">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/app_bar_head">
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            android:id="@+id/collapse_toolbar">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/realup"
                android:id="@+id/header_image"/>


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

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

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



<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/recycle"
    android:layout_below="@+id/header"/>

</RelativeLayout>

如您所见,我有一个 recyclerview,我希望当 recyclerview 向下滚动时, ImageView 会随着 recyclerview 的滚动而折叠。你能帮助我吗?预先感谢您:)

最佳答案

您的 RecyclerView 应位于 Coordinatorlayout 中以执行动画并使用 NestedScrollView 作为 RecyclerView 的父级:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="realup.ir.teacher.MainActivity">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/app_bar_head">
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            android:id="@+id/collapse_toolbar">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/realup"
                android:id="@+id/header_image"/>


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

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

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

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

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

要获得平滑的滚动效果,请在代码中添加:

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycle);
recyclerView.setNestedScrollingEnabled(false);

希望这有帮助。

抱歉我的英语不好。

关于android - 通过滚动回收器 View 折叠标题图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45635774/

相关文章:

android - 分 ionic 项、更新并附加?安卓引擎

android - 任务之间的 Gradle 依赖关系

android - RecyclerView 中的 Android Spinners 问题

android - 从其他 Activity 返回到同一 fragment 时,如何保存和恢复 fragment 中 RecyclerView 的滚动位置?

javascript - Angular Material - 为什么我的 md-button 是全 Angular 的?

android - 在 Android 上使用 SVG 图标的最佳做法是什么?

安卓小工具 : OnClickPendingIntent won't Work

android - 如何在其适配器的 onBindViewHolder 中更新 recyclerView?

android - 将 View 移出约束布局

android - RippleDrawable 不在 View 上绘制