首先实现一个头部固定的ExpandedListView,然后在它的基础上实现:在头部加一个背景图片,默认状态下他处于展开状态,往上滑的时候背景图片逐渐的折叠起来,往下滑的时候背景图片慢慢的展开效果图如下:

通过CoordinateLayout实现的折叠式布局
有人可能会说这不就是折叠式布局吗?是的,这就是Android 5.0给我们提供的材料设计库中的CoordinateLayout就是解决这个问题的,使用CoordinateLayout来协调ScrollView,NestedScrollView,ListView,RecycleView和顶部的背景图片、ToolBar之间的滚动关系、在很多的手机应用中,时不时会看到关于折叠布局的效果,现在我们先看看CoordinateLayout是怎么实现的然后在讲我们自定义实现一个折叠式布局,直接上代码:
<?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:layout_width="match_parent"
android:layout_height="200dp">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="https://blog.csdn.net/@mipmap/homepage_pic_banner"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/view_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="新闻详情" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="hello world" />
...
...
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

以上就是实现一个折叠式布局的典型模板布局代码,一个简简单单的布局就实现了这样的效果,但是必须要注意在AndroidMnifest.xml必须要给Activity指定它的theme为NoActionBar的样式代码如下:
<activity
android:name=".test.CoordinatorLayoutTestActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
否则会出现ActionBar和ToolBar共存的情况,的显示效果如下:

本文详细介绍了如何在Android开发中实现折叠式布局,通过CoordinatorLayout、AppBarLayout和CollapsingToolbarLayout的组合使用,展示了折叠效果的实现过程。内容包括ToolBar设置、滑动控件配置、折叠布局相关属性的解释以及监听折叠状态的方法。此外,还讨论了自定义折叠式布局StickyLayout的实现,分析了事件拦截和滑动冲突处理,提供了关键代码示例。
1107

被折叠的 条评论
为什么被折叠?



