(四) 仿支付宝首页顶部伸缩滑动/中间层下拉刷新

效果图如下:


demo4.gif

主布局文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    tools:context=".demo4.Demo4Activity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_coali_behavior">
     <!--appbar_coali_behavior自定义behavior实现下拉刷新功能-->
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="false"
            android:clipToPadding="false"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

             <!--toolbar下方四个按钮布局--> 
            <include
                layout="@layout/layout_toolbar_content"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dimen_80"
                android:layout_marginTop="@dimen/dimen_50"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />


           <!--包含刷新view的布局--> 
            <include
                android:visibility="visible"
                layout="@layout/layout_middle_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_130"
                app:layout_collapseMode="pin" />

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="@dimen/dimen_50"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">
                  <!--展开时的toolbar-->
                <include layout="@layout/layout_toolbar_normal" />
               <!--折叠后的toolbar-->
                <include layout="@layout/layout_toolbar_collapse" />

            </android.support.v7.widget.Toolbar>


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

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

    <include
        layout="@layout/layout_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

子布局 layout_middle_content

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center_vertical"
            android:minHeight="80dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="余额宝"
                android:textColor="@color/black33"
                android:textSize="12sp" />

            <TextView

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="转账"
                android:textColor="@color/black33"
                android:textSize="12sp" />

            <TextView

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="红包"
                android:textColor="@color/black33"
                android:textSize="12sp" />

            <TextView

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="充值中心"
                android:textColor="@color/black33"
                android:textSize="12sp" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center_vertical"
            android:minHeight="80dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="AA收款"
                android:textColor="@color/black33"
                android:textSize="12sp" />

            <TextView

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="城市服务"
                android:textColor="@color/black33"
                android:textSize="12sp" />

            <TextView

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="蚂蚁森林"
                android:textColor="@color/black33"
                android:textSize="12sp" />

            <TextView

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableTop="@mipmap/icon_zfb"
                android:gravity="center_horizontal"
                android:text="更多"
                android:textColor="@color/black33"
                android:textSize="12sp" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="@color/grayF5" />

           <!--下拉刷新view--> 
        <LinearLayout
            android:background="@color/grayF5"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:tag="refresh">
    
            <com.dl.mybaseproject.demo4.SmileView
                android:id="@+id/smile_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="-10dp"
                app:smil_line_width="3"
                app:smil_radius="10" />

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

实现顶部伸缩渐变利用 appBar.addOnOffsetChangedListener,在其监听方法中通过改变背景的透明度来控制。

   @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        //垂直方向偏移量
        int offset = Math.abs(verticalOffset);
        //最大偏移距离
        int scrollRange = appBarLayout.getTotalScrollRange();
        /*本来应该取scrollRange/2  由于appbar包含三部分 Toolbar ToolbarContent 与middleContent
            此处用height代替  height为Toolbar和ToolbarContent的综合  由于ToolbarContent的layout_collapseMode为
            parallax,且layout_collapseParallaxMultiplier为0.7 所有此处做了0.7的处理
        **/
        int height = layoutToolbarNormal.getHeight() + layoutToolbarContent.getHeight() * 7 / 10;
        if (offset <= height / 2) {//当滑动没超过一半,展开状态下toolbar显示内容,根据收缩位置,改变透明值
            layoutToolbarNormal.setVisibility(View.VISIBLE);
            layoutToolbarCollapse.setVisibility(View.GONE);
            //根据偏移百分比 计算透明值
            float scale2 = (float) offset / (height / 2);
            int alpha2 = (int) (255 * scale2);
            bgToolbarNormal.setBackgroundColor(Color.argb(alpha2, 26, 128, 210));
        } else if (offset <= height) {//当滑动超过一半,收缩状态下toolbar显示内容,根据收缩位置,改变透明值

            layoutToolbarCollapse.setVisibility(View.VISIBLE);
            layoutToolbarNormal.setVisibility(View.GONE);
            float scale3 = (float) (height - offset) / (height / 2);
            int alpha3 = (int) (255 * scale3);
            bgToolbarCollapse.setBackgroundColor(Color.argb(alpha3, 26, 128, 210));
        } else {
            bgToolbarCollapse.setBackgroundColor(Color.argb(0, 26, 128, 210));
        }

        //根据偏移百分比计算扫一扫布局的透明度值
        float scale = (float) offset / (scrollRange/2);
        int alpha = (int) (255 * scale);
        bgToolbarContent.setBackgroundColor(Color.argb(alpha, 26, 128, 210));

    }

通过自定义appBarLayout的behavior来实现下拉刷新

  //获取刷新布局高度
    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout abl, int layoutDirection) {
        boolean handled = super.onLayoutChild(parent, abl, layoutDirection);

        // 需要在调用过super.onLayoutChild()方法之后获取
        if (mTargetView == null) {
            mTargetView = parent.findViewWithTag(TAG);
            mTargetView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    mTargetView.getViewTreeObserver().removeOnPreDrawListener(this);
                    //初始化刷新高度以及控件高度
                    TARGET_REFESH_HEIGHT = getCalHeight(mTargetView);
                    mTargetViewHeight = TARGET_REFESH_HEIGHT;
                    return false;
                }
            });
            if (mTargetView != null) {
                initial(abl);
            }
        }

        return handled;
    }

//判断滑动是否执行布局下拉展开
   @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        if (!isRecovering) {
            //返回1时,表示当前target处于非touch的滑动,
            //该bug的引起是因为appbar在滑动时,CoordinatorLayout内的实现NestedScrollingChild2接口的滑动子类还未结束其自身的fling
            // 所以这里监听子类的非touch时的滑动,然后block掉滑动事件传递给AppBarLayout
            if (mTargetView != null && ((dy < 0 && child.getBottom() >= mParentHeight )
                    || (dy > 0 && child.getBottom() > mParentHeight)) && !isPining && type!=1) {
                scale(child, dy, target);//执行下拉展开动画
                return;
            }
        }
    }

//判断是否开始刷新动画
    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target, int type) {

        if (checkPin()) {//检测是否达到刷新临界值
            startPin();//开始刷新动画
        } else {
            recovery(abl);
        }
        super.onStopNestedScroll(coordinatorLayout, abl, target, type);
    }

(一)高斯模糊实现毛玻璃效果丶共享元素动画 丶地址选择器
(二)仿京东顶部伸缩渐变丶自定义viewpager指示器丶viewpager3D回廊丶recyclerview瀑布流
(三)RxJava2常用操作符merge、flatmap、zip--结合MVP架构讲解
(五)TabLayout+ViewPager悬浮吸顶及刷新数量动画显示
(六)仿QQ首页drawer/侧滑删除/浮动imgaeView/角标拖拽
(七)仿微信发布朋友圈拖拽删除

将持续更新.. 不喜勿喷,仅个人分享,希望能帮助到你

源码地址:Github传送门

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,714评论 2 59
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,078评论 25 707
  • 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayo...
    小狼W阅读 1,613评论 0 10
  • 飞花落梦半生寒,天可见,有谁怜? 豪情万丈悲白发,穷毕生,泪不沾! 为国何享君王禄,风波一叹笑馋奸,万民都哭遍,肝...
    易郁生阅读 206评论 0 0
  • 刚看到新闻很惊骇,大过年的,给老婆和两个孩子买了动物园的票,自己翻墙逃票。却未想墙那头是老虎区,莫名葬送了生命。本...
    不讲道理薇阅读 345评论 9 4