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

效果图如下:


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传送门

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

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

友情链接更多精彩内容