先看效果。
源码:GitHub地址
实现与分析
很显然,这样的效果用到了Android Material Design里的控件,分别是CoordinatorLayout和AppBarLayout。其中,AppBarLayout控件便具备顶部固定的功能,但它需要被CoordinatorLayout嵌套起来才能实现滚动固定的效果,否则无效。
上面效果图的Layout代码如下:
<?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:focusable="true"
android:focusableInTouchMode