自定义behavior-仿华为应用市场

本文介绍如何利用Behavior配合CoordinatorLayout实现类似华为应用市场首页的交互效果,包括搜索框跟随Banner滑动和导航栏颜色渐变。通过自定义Behavior简化滑动事件处理,代码实现简单,提供源码供参考。
摘要由CSDN通过智能技术生成

Behavior 配合 CoordinatorLayout 可以实现很多酷炫的交互,掌握自定义 Behavior 也是很大的提升 。先看看要实现的效果如下:

图1

上面这个效果是「华为应用市场」的首页效果。使用 RecycleView(ListView)也是可以实现的。不过需要对滑动事件的处理相对较多。如果使用自定义 behavior 就不需要那么复杂了。

自定义 behavior 有两种

  1. 一个 view 依赖另外一个 view。(A 依赖于 B ,B 的状态发生变化,A 也发生变化)
  2. 一个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&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值