Behavior 配合 CoordinatorLayout 可以实现很多酷炫的交互,掌握自定义 Behavior 也是很大的提升 。先看看要实现的效果如下:
上面这个效果是「华为应用市场」的首页效果。使用 RecycleView(ListView)也是可以实现的。不过需要对滑动事件的处理相对较多。如果使用自定义 behavior 就不需要那么复杂了。
自定义 behavior 有两种:
- 一个 view 依赖另外一个 view。(A 依赖于 B ,B 的状态发生变化,A 也发生变化)
- 一个view 监听 CoordinateLayout 中嵌套子 View 的滑动状态。
观察上面的效果可以很明显的发现: 搜索框和 导航栏都是跟随 Banner 的滑动做出相应变化。这里我们使用第一种方法来完成。(自定义搜索框可以查看源码)
先看看布局:
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.bigkoo.convenientbanner.ConvenientBanner
android:id="@+id/convenientBanner"
android:layout_width="match_parent"
android:layout_height="@dimen/bannner_height"
app:canLoop="true"
app:layout_collapseMode="pin" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar&