使用CoordinatorLayout+Behavior实现一个

作者: NewNiu | 来源:发表于2019-05-31 11:07 被阅读24次

    本文对应Git项目

    我实现的效果如下:

    (图1)头部不带开关锁 (图2)头部带开关锁

    啥是CoordinatorLayout?

    其实它就叫做:协调布局容器(个人解释),就是协调它内部的view的摆放位置以及每个view的依赖关系。这个不难理解,意思是一个视图A在处理滑动或别的事件的时候,依赖于视图A的视图B随着A的位移而位移或更多别的操作的变化。
    CoordinatorLayout要做为需要处理依赖关系来交互操作的View的根布局,并需要给它对应的第一级子View设置Behavior。如下:

    <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"
        tools:context=".MainActivity">
        <!-- 这部分为HeaderView部分-->
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/header_behavior">
              ........
        <!-- 可放置任意多个View做为头部中的View -->
        </android.support.v4.widget.NestedScrollView>
    
        <!-- 搜索框 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/home_header_searchview_height"
            android:orientation="vertical"
            app:layout_behavior="@string/header_searchview_behavior">
    
            <android.support.v7.widget.CardView
                android:id="@+id/search_view"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="@dimen/dp_5"
                android:background="@color/transparent"
                app:cardCornerRadius="@dimen/dp_6"
                app:cardElevation="@dimen/dp_4"
                app:cardMaxElevation="@dimen/dp_4">
    
                <TextView
                    android:id="@+id/search_text"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="搜索或输入网址"
                    android:textColor="@color/_969696"
                    android:textSize="@dimen/sp_15" />
    
                <LinearLayout
                    android:id="@+id/ll_speech"
                    android:layout_width="@dimen/dp_40"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:gravity="center"
                    android:orientation="vertical">
    
                    <ImageView
                        android:id="@+id/speech"
                        android:layout_width="@dimen/dp_20"
                        android:layout_height="@dimen/dp_20"
                        android:scaleType="centerCrop"
                        android:src="@mipmap/icon_speech_voice_home" />
                </LinearLayout>
    
            </android.support.v7.widget.CardView>
         </LinearLayout>
        <!-- 最底部Recyclerview列表 -->
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/news_flow_nsv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            app:layout_behavior="@string/header_newsflow_behavior">   <!-- 该Behavior为CoordinatorLayout的直接子View的属性 -->
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/news_flow_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </android.support.v4.widget.NestedScrollView>
         <TextView
              android:id="@+id/bottombar"
              android:layout_width="match_parent"
              android:text="bottombar"
              android:background="#f40"
              android:layout_gravity="bottom"
              android:layout_height="@dimen/home_bottombar_height" />
    </android.support.design.widget.CoordinatorLayout >
    

    说到这里,需要知道这些写在XML中的app:layout_behavior属性值的设置。
    这个属性值的来源:

    • 我们自己自定义的Behavior,需要继承CoordinatorLayout.Behavior<T>,这里的T表示在XML中需要设置layout_behavior的那个view。也可以直接写View.
    • Android系统目前提供给我们的自带Behavior
    Behavior的依赖关系一般分为两种情况

    自定义Behavior必需要实现其带有两个参数的构造方法!!!否则Behavior将不起效果!

    1. View-B依赖View-A,View-B根据View-A的位置、尺寸等状态的改变而改变。

    这里,我们利用(图1)来说明一下,图中的搜索框就是根据一个Header View来实现依赖后滑动的。这里的Header除了图中最下方的Recyclerview以外,其余都是头部。将搜索框设置好初始位置以后就可以根据HeaderView的变化而变化了。后面我针对这个HeaderView做一个详细说明。
    这里需要回调两个方法:
    1)public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {}
    2)public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull View dependency) {}

    1. 某个View监听CoordinatorLayout中实现了NestedScrollingChild的View移动偏移量来改变其自身的大小和位置等属性。例如:Recyclerview实现了NestedScrollingChild2接口,NestedScrollingChild2又继续了NestedScrollingChild接口。

    这里除了最底部的RecyclerView以外,上面其它View都属于头部视图,这里的头部视图的滑动其实就是根据Behavior中的
    1.onStartNestedScroll()// 该方法主要作用为判断用户滑动的手势或方向
    2.onNestedPreScroll()
    3.onNestedScroll()等方法来实现控制滑动的。
    注意:
    只要当前CoordinatorLayout中使用了Behavior,且每个Behavior中如果都实现了上述以个方法的话,当CoordinatorLayout中存在实现了NestedScrollingChild接口的View的话,手指在屏幕上滑动后,所有的Behavior中重写了上述方法的这些回调方法都将会同时触发!!所以当你发现如果View在移动的过程中出现速度超过手指移动速度以后,基本原因就是你在多个Behavior的嵌套回调方法中设置了View的移动,这样你移动的速度就翻倍了。


    讲解HeaderView部分

    这里的HeaderView部分指的是除了最下面的RecyclerView+Bottombar+SearchView外的其余部分。
    显然,我们头部已经远远超过了当前屏幕的最大高度。因为我们的头部图片区已经很高了,再加上里面一个20条数据的RecyclerView的9宫格和一个Viewpager。

    为什么把这么多的View都写在一个Header里面呢?

    可以看到,我们的首页中存在有2个Recyclerview,如果2个RecyclerView都存在的情况下,肯定会出现嵌套滑动冲突的情况!这里,我让其中一个RecyclerView失去嵌套滑动能力。

    // 找到nested_header,这里的NestedScrollView是包裹上面那个Recyclerview的父容器
    NestedScrollView nested_header = findViewById(R.id. nested_header)
    // 设置平滑滚动关闭,设置以后,滑动冲突即解决了。
    nested_header.setSmoothScrollingEnabled(false);
    // 以上为第一部要解决的
    

    其次,我们在设置搜索框的时候,由于搜索框是悬浮在整个Header之上并且依赖于Header的滑动而控制滑动距离的,所以我们需要获取并设置Header的真实实际高度。
    由于我在Header中放置了多个View,这里计算Header的实际高度到时候需要用到(具体代码可以查看项目中的HeaderBehavior->onMeasureChild()方法)

    // 用于计算并设置当使用前Behavior的View的尺寸
    @Override
        public boolean onMeasureChild(
    @NonNull CoordinatorLayout parent, 
    @NonNull View child, 
    int parentWidthMeasureSpec, 
    int widthUsed, 
    int parentHeightMeasureSpec,
    int heightUsed) {
        // 首先需要获取当前容器View(child)中所有View的margin值和其本身的高度值并相加,
        // 如果存在Recyclerview的话,要计算每一行的高度并相加
    
    int itemHeight = 0;
            int childViewHeight = 0;
    
            NestedScrollView nestedScrollView = (NestedScrollView) child;
            int nestedChildCount = nestedScrollView.getChildCount();
    
            for (int z = 0; z < nestedChildCount; z++) {
                final View nestedChildView = nestedScrollView.getChildAt(z);
                Log.d("ddd", "第一层for,z ...... nestedChildView != null && nestedChildView instanceof Relativelayout ?  --> " + (nestedChildView != null && nestedChildView instanceof RelativeLayout));
                if (nestedChildView != null && nestedChildView instanceof RelativeLayout) {
                    RelativeLayout relativeLayout = (RelativeLayout) nestedChildView;
                    int childCount = relativeLayout.getChildCount();
                    Log.d("ddd", "childCount == " + childCount);
                    for (int i = 0; i < childCount; i++) {
                        final View childView = relativeLayout.getChildAt(i);
                        // 计算子View的高度和margin高度
                        RelativeLayout.LayoutParams rlpc = (RelativeLayout.LayoutParams) childView.getLayoutParams();
                        int ctm = rlpc.topMargin;
                        int cbm = rlpc.bottomMargin;
                        int chm = rlpc.height;
                        childViewHeight = childViewHeight + ctm + cbm + chm;
    
    //                    final View childView = child.getChildAt(i);
                        Log.d("ddd", "第二层for,i....... childView != null && childView instanceof RecyclerView ?--》  " + (childView != null && childView instanceof RecyclerView));
                        if (childView != null && childView instanceof RecyclerView) {
                            RecyclerView recyclerView = (RecyclerView) childView;
                            RecyclerView.Adapter adapter = recyclerView.getAdapter();
                            Log.d("ddd", "recyclerview.getadapter == null? " + (recyclerView.getAdapter()));
                            if (adapter != null) {
                                int itemCount = adapter.getItemCount();
                                for (int j = 0; j < itemCount; j++) {
                                    if (j % 4 == 0) {// 判断当前位置是否是每行第一个View的位置
                                        View childAt = recyclerView.getChildAt(j);
                                        if (childAt != null) {
                                            Log.d("ddd", "child item height : " + childAt.getMeasuredHeight());
                                            itemHeight += childAt.getMeasuredHeight();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            int totalHeight = childViewHeight + itemHeight;
    
            int heightSpec = View.MeasureSpec.makeMeasureSpec(totalHeight, View.MeasureSpec.EXACTLY);// 将计算得来的Child的实际高度转换成measure可接收识别的measureSpec规格
            child.measure(parentWidthMeasureSpec, heightSpec);
            return true;
    }
    

    以上,即为将Header的高度设定死了!后面用于被别的View依赖的时候就可以直接通过dependency.getMeasureHeight()方法来获取到其的真实高度了。这里我写了好几个for循环,就是为了找到里面存在的RecyclerView所在的层级,找到它以后就可以通过mRecyclerview.getAdapter来获取里面的一共多少条数据了,也就可以根据需要计算每一行每一个View的实际高度了,这样一加,最后整个RecyclerView的高度就知道了!--还有不能忘记每个View设置的margin值,特别是marginTop/marginBottom!


    HeaderView的偏移

    HeaderView的高度设置好了,我们接下来就需要让它可以通过我们手势来滑动让其偏移位置了

    由于嵌套滑动回调事件不能在所有Behavior中随意写,容易出现滑动监听重复问题。所以我在自己demo中,写在了依赖HeaderView的最底部RecyclerView中所设置的NewsFlowBehavior中。
    在该Behavior中如果需要控制其Dependency(Recyclerview依赖的那个View)的话,是需要提前获取到Dependency的。因为在嵌套滑动监听回调方法中是没有依赖的View返回的。

    这里需要注意几点问题

    1.想让一个没有任何依赖的View滑动就需要调用嵌套回调,xml布局里面必须要有一个实现了NestedScrollingChild接口的View,这样就能在Behavior中回调嵌套调用如下几个方法:(这几个方法返回的一般都是手指在屏幕上触摸滑动以后的方向及距离值什么的)

    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {}
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {}
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {}
    

    以上几个方法的回调必须要触摸在实现了NestedScrollingChild的View上!类似Linearlayout这样的容器没有实现又想让触摸它的时候有这些回调怎么办?那就在它的外层再包上一个NestedScrollView容器(v4包下)。

    public class NewsFlowBehavior extends CoordinatorLayout.Behavior<View> {
    
        private Context mContext;
        private WeakReference<View> dependency;
        private WeakReference<View> child;
    
        private Scroller mScroller;
        private Handler mHandler;
    
        public NewsFlowBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.mContext = context;
            this.mHandler = new Handler();
            this.mScroller = new Scroller(context);
        }
        // 告诉当前Behavior只有手势为上下滑动的时候才执行嵌套监听
        @Override
        public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
            // 判断是否在垂直方向上滑动
            return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
        }
        // 嵌套准备滑动
        @Override
        public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
            View dependency = getDependency();
            float dty = dependency.getTranslationY();
            float minHeaderScrollDistance = -(dependency.getMeasuredHeight() - getSearchViewHeight());
    
            // 防界面快速滑动抖动-针对fling的情况
            if (type == ViewCompat.TYPE_NON_TOUCH && (dty == 0 || dty <= minHeaderScrollDistance )) {
                ViewCompat.stopNestedScroll(target, type);
            }
            // 当dy<0的时候说明 手指自上向下滑动
            if (dy < 0) {// 如果是自上向下滑动即不处理
                return;
            }
            // 说明该方法处理手指自下向上滑动的事件
            float dTranslationY = dependency.getTranslationY() - dy;
            float dMinTranslationY = -(dependency.getHeight() - getSearchViewHeight());
            if (dTranslationY > dMinTranslationY) {
                dependency.setTranslationY(dTranslationY);
                consumed[1] = dy;
            } else {
                dependency.setTranslationY(dMinTranslationY);
            }
            onFlingFinished(child);// 滑动结束以后处理自动滑动事件
        }
    
        @Override
        public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
           // dyUnconsumed > 0 说明是手指自下向上滑动
            if (dyUnconsumed > 0) {
                return;
            }
           // 这里处理手指自上向下滑动事件
            View dependency = getDependency();
            float dTranslationY = dependency.getTranslationY() - dyUnconsumed;
            float dMaxTranslationY = 0;
            if (dTranslationY < dMaxTranslationY) {
                dependency.setTranslationY(dTranslationY);
            } else {
                dependency.setTranslationY(0);
            }
        }
        // 当手指触摸到屏幕或离开屏幕的时候触发
        @Override
        public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
            mScroller.abortAnimation();// 禁用自动滑动动画
            isScrolling = false;
            super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, axes, type);
        }
    
        // 当嵌套滑动停止以后
        @Override
        public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int type) {
            // 如果手指从屏幕上拿开且并不是正在fling
            if (!isScrolling) {
                float dependencyTY = getDependency().getTranslationY();
                float headerScrollDistance = getHeaderHeight() - getSearchViewHeight();
                float anchorPoint = -(headerScrollDistance / 3);// 获取上下滚动的分界点 小于这个点则向上滚动,大于这个点向下滚动
                if (dependencyTY == 0 || dependencyTY == -headerScrollDistance) {
                    return;
                }
                float velocityY = 800.f;// 速度800
                // true: 让dependency view向上滚动
                boolean isUpWared = dependencyTY < anchorPoint;
    
                FlingRunnable flingRunnable = new FlingRunnable(child);
    
                if (dependencyTY < 0 && dependencyTY > -headerScrollDistance) {
                    float targetTranslationY = isUpWared ? (-headerScrollDistance) : 0;
                    mScroller.startScroll(0, (int) dependencyTY, 0, (int) (targetTranslationY - dependencyTY), (int) velocityY);
                    mHandler.post(flingRunnable);
                    isScrolling = true;
                } else {
                    float dependencyMeasureHeight = getDependency().getMeasuredHeight();// dependency 的实际高度
                    float minDependencyScrollTY = -(dependencyMeasureHeight - getSearchViewHeight());// dependency 可滑动到的最小位置
                    float dependencyScrollDistance = dependencyMeasureHeight - getToplinesheight() - getSearchViewHeight();
                    float toplineScrollAnchorPoint = getToplinesheight() / 3;// 今日头条高度的三分之一,用作于dependency自动滑动的锚点
                    float dependencyAutoScrollLocation = -dependencyScrollDistance - toplineScrollAnchorPoint;// dependency 开始自动滑动的位置
    
                    // true:让dependency view 自动上移,将child移动到searchview下方
                    // false:让dependency view 自动下移,将topline移出来
                    boolean isToplineUpwared = dependencyTY < dependencyAutoScrollLocation;
    
                    if (dependencyTY < -dependencyScrollDistance && dependencyTY > minDependencyScrollTY) {
                        float dependencyTranslationY = isToplineUpwared ? minDependencyScrollTY : -dependencyScrollDistance;
                        mScroller.startScroll(0, (int) dependencyTY, 0, (int) (dependencyTranslationY - dependencyTY), (int) velocityY);
                        mHandler.post(flingRunnable);
                        isScrolling = true;
                    }
                }
    //            onFingerStopDrag(child,800);
            }
        }
    
        private boolean onFingerStopDrag(View child, float velocityY) {
            View dependentView = getDependency();
            float dependentViewTranslationY = dependentView.getTranslationY();
            float minDependentViewScrollOffset = -(getHeaderHeight() - getSearchViewHeight());// 自动滑动最小值 minDependentViewScrollOffset = -430
            Log.e("ddd", "dependent View TranslationY ---= " + dependentView.getTranslationY() + "   min Dependent view scroll offset ----: " + minDependentViewScrollOffset);
            if (dependentViewTranslationY == 0 || dependentViewTranslationY == minDependentViewScrollOffset) {
                return false;
            }
            boolean targetState;
            boolean isUpward = velocityY > 0;
            if (dependentViewTranslationY > minDependentViewScrollOffset && dependentViewTranslationY < 0) {
                float targetTranslationY = isUpward ? minDependentViewScrollOffset : 0;
    
                mScroller.startScroll(0, (int) dependentViewTranslationY, 0, (int) (targetTranslationY - dependentViewTranslationY), (int) (1000000 / Math.abs(velocityY)));
                mHandler.post(new FlingRunnable(child));
                isScrolling = true;
                return true;
            }
            return false;
        }
    
        private class FlingRunnable implements Runnable {
            private final View mlayout;
    
            public FlingRunnable(View layout) {
                this.mlayout = layout;
            }
    
            @Override
            public void run() {
                if (mScroller.computeScrollOffset()) {
                    getDependency().setTranslationY(mScroller.getCurrY());
                    mHandler.post(this);
                } else {
                    isScrolling = false;
                    onFlingFinished(mlayout);
                }
            }
        }
    
        private void onFlingFinished(View layout) {
            changeState(isClosed(layout) ? STATE_CLOSED : STATE_OPENED);
        }
    
        private float getHeaderHeight() {
            return mContext.getResources().getDimension(R.dimen.home_header_height);
        }
    
        private float getSearchViewHeight() {
            return mContext.getResources().getDimension(R.dimen.home_header_searchview_height);
        }
    
        private float getBottombarHeight() {
            return mContext.getResources().getDimension(R.dimen.home_bottombar_height);
        }
    
        private float getToplinesheight() {
            return mContext.getResources().getDimension(R.dimen.home_body_header_topline_height);
        }
    
        private View getDependency() {
            return dependency.get();
        }
    
        private View getChild() {
            return child.get();
        }
    

    以上部分则为HeaderView部分的移动。getDependency()即为获取该RecyclerView依赖的那个View,依赖方法在下面这部分代码:

    依赖于HeaderView的Recyclerview和搜索框的偏移

    HeaderView已经可以移动了,那么现在依赖它的View也需要做一些处理了
    RecyclerView的偏移

    这部分代码和上部分移动HeaderView是一起的,这里包含使用该Behavior的Recyclerview依赖HeaderView的回调,为了区分功能我抽了出来写blog

    // 设置当前Behavior依赖于哪个View
    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull View dependency) {
        return false;
    }
    // 根据依赖的View(dependency)的位置、大小变化改变自己的位置、大小的变化
    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull View dependency) {
        return false;
    }
    
    public class NewsFlowBehavior extends CoordinatorLayout.Behavior<View> {
    
        private Context mContext;
        private WeakReference<View> dependency;
        private WeakReference<View> child;
    
        private Scroller mScroller;
        private Handler mHandler;
    // 由于当前Recyclerview显示的高度是MATCH_PARENT,所以如果不重新设置其高度的话,当列表滑动到最底部后,会有1到2条数据在屏幕之外或被底部Bottombar遮挡,这部分用来计算child的高度
        @Override
        public boolean onMeasureChild(@NonNull CoordinatorLayout parent, @NonNull View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
            // 获取屏幕高度
            int screenHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);
            // 计算child的最大高度
            // child最大高度 = 屏幕高度 - 搜索框高度 - 底部菜单高度
            int maxChildHeight = (int) (screenHeight - getSearchViewHeight() - getBottombarHeight());
            parentHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxChildHeight, View.MeasureSpec.EXACTLY);
            child.measure(parentWidthMeasureSpec, parentHeightMeasureSpec);
            return true;
        }
    
    // 该方法用于在被依赖的Dependency发生变化后来处理child的变化用的,这部分用来设置child的位置
        @Override
        public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
            // 计算HeaderView中的图片部分View在屏幕上移动的比例
            float scale = 1.f - Math.abs(dependency.getTranslationY() / dependency.getHeight());
            // 初始化位置 = dependency的高度
            float initHeight = dependency.getHeight();
            // 计算当前Child应该在的位置
            float childTranslationY = scale * initHeight;
            if (dependency.getTranslationY() <= -(dependency.getHeight() - getSearchViewHeight())) {
                child.setTranslationY(getSearchViewHeight());
            } else {
                child.setTranslationY(childTranslationY);
            }
            return true;
        }
    
        // 该方法用于告诉使用当前Behavior的View要依赖的View
        @Override
        public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
            if (dependency != null && dependency.getId() == R.id.nested_header) {// 指依赖头部view
                this.dependency = new WeakReference<>(dependency);
                this.child = new WeakReference<>(child);
                return true;
            }
            return super.layoutDependsOn(parent, child, dependency);
        }
    }
    

    以上便是Recyclerview的初始化设置和根据HeaderView的偏移而移动的功能代码了

    搜索框的位移和初始化

    我们看到搜索框只在整个HeaderView的上部分进行偏移,并且搜索框中的文本还有一个左右的偏移的效果

    移动这个搜索框需要

    public class SearchViewBehavior extends CoordinatorLayout.Behavior<View> {
        private Context mContext;
        private ArgbEvaluator mArgbEvaluator;
    
    
        public SearchViewBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.mContext = context;
            this.mArgbEvaluator = new ArgbEvaluator();
        }
    
        @Override
        public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
            if (dependency != null && dependency.getId() == R.id.nested_header) {
                return true;
            }
            return super.layoutDependsOn(parent, child, dependency);
        }
    
        @Override
        public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
    
            float scrollDistanceScale = 1.f - Math.abs(dependency.getTranslationY() / (getHeaderHeight() - getSearchViewHeight()));
            Log.i("gudd", "searchViewBehavior scrollDistanceScale : " + scrollDistanceScale);
            float childInitHeight = getHeaderHeight() - getSearchViewHeight();
            float childTranslationY = childInitHeight * scrollDistanceScale;
            if (dependency.getTranslationY() <= -(getHeaderHeight() - getSearchViewHeight())) {
                child.setTranslationY(0);
                setChildAnimator(0, child);
            } else {
                child.setTranslationY(childTranslationY);
                setChildAnimator(scrollDistanceScale, child);
            }
    
            return true;
        }
    
        /**
         * 设置search textview 的联动动画
         *
         * @param scrollProgress 滑动进度
         * @param child          当前的searchview部分
         */
        private void setChildAnimator(float scrollProgress, View child) {
            // 设置child背景透明色 从完全透明到完全不透明
            child.setBackgroundColor((int) mArgbEvaluator.evaluate(
                    scrollProgress,
                    child.getResources().getColor(R.color._f2f2f2),
                    child.getResources().getColor(R.color._00ffffff)));
    
            View subChild = getSubChildOfChild(child);
            if (subChild != null && subChild instanceof CardView) {
                // 设置cardview
                subChild.setBackgroundColor((int) mArgbEvaluator.evaluate(
                        scrollProgress,
                        child.getResources().getColor(R.color._ffffff),
                        child.getResources().getColor(R.color._80ffffff)));
    
    
                // 设置margin
                float collapsedMargin = subChild.getResources().getDimension(R.dimen.home_search_collapsing_margin);
                // 设置marginleft or right值
                float initMargin = subChild.getResources().getDimension(R.dimen.home_search_view_initmargin);
                int margin = (int) (collapsedMargin + (initMargin - collapsedMargin) * scrollProgress);
    
                LinearLayout.LayoutParams cl = (LinearLayout.LayoutParams) subChild.getLayoutParams();
                int marginTop = cl.topMargin;
                int marginBottom = cl.bottomMargin;
                cl.setMargins(margin, marginTop, margin, marginBottom);
                subChild.setLayoutParams(cl);
    
                setSearchTextViewAnimator(scrollProgress, subChild,child);
            }
    
        }
    
        private View getSubChildOfChild(View child) {
            // 获取child 里面search textview 的视图
            if (null != child && child instanceof LinearLayout) {
                LinearLayout childOfLL = (LinearLayout) child;
                int childCount = childOfLL.getChildCount();
                if (childCount > 0) {
                    for (int i = 0; i < childCount; i++) {
                        return childOfLL.getChildAt(i);
                    }
                }
            }
            return null;
        }
    
        /**
         *
         * @param scrollProgress 滑动进度
         * @param subChild 此入为child的第一层子view   cardview
         * @param child
         */
        private void setSearchTextViewAnimator(float scrollProgress, View subChild, View child) {
    //        View subView = getSubChildOfChild(child);
            if (null != subChild & subChild instanceof CardView) {
                CardView cardView = (CardView) subChild;
                int childCount = cardView.getChildCount();
                if (childCount > 0) {
                    for (int i = 0; i < childCount; i++) {
                        View childView = cardView.getChildAt(i);
                        if (null != childView && childView instanceof TextView) {
                            TextView textView = (TextView) childView;
                            int textMeasureWidth = textView.getMeasuredWidth();
                            // 计算child的宽度
                            int childMeasuredWidth = child.getMeasuredWidth();
                            // 获取当前view所在父容器中的距左距离
                            int initMargin = (childMeasuredWidth - textMeasureWidth) / 2;
                            float subMarginLeft = textView.getResources().getDimension(R.dimen.home_search_collapsing_margin);
    
                            LinearLayout.LayoutParams cl = (LinearLayout.LayoutParams)cardView.getLayoutParams();
                            float cardviewMarginLeft = cl.leftMargin;
    
                            int margin = (int) (subMarginLeft + (initMargin - cardviewMarginLeft - subMarginLeft) * scrollProgress);
                            Log.i("ddd", "textMeasureWidth : "+textMeasureWidth+"   childMeasuredWidth : "+childMeasuredWidth+"  scrollProgress : "+scrollProgress +"  margin : " + margin);
    //                        textView.setTranslationX(margin);
                            CardView.LayoutParams ll = (CardView.LayoutParams) textView.getLayoutParams();
                            ll.setMargins(margin,ll.topMargin,ll.rightMargin,ll.bottomMargin);
                            textView.setLayoutParams(ll);
                        }
                    }
                }
            }
        }
    
        // 获取从searchview底部往上的到屏幕的高度
        private float getHeaderHeight() {
            return mContext.getResources().getDimension(R.dimen.home_header_height);
        }
    
        // 搜索框的高度
        private float getSearchViewHeight() {
            return mContext.getResources().getDimension(R.dimen.home_header_searchview_height);
        }
    }
    

    根据以上代码,一个类似新闻首页的View就实现了。这里要了解Behavior里面第个回调的含意,代码理解就不难了!有什么问题可以留言。

    相关文章

      网友评论

        本文标题:使用CoordinatorLayout+Behavior实现一个

        本文链接:https://www.haomeiwen.com/subject/jxhjtctx.html