反编译简书app和小红书app滑动效果sticky粘性头布局的实现CoordinatorLayout+behavior

反编译简书app和小红书app滑动效果sticky粘性头布局的实现CoordinatorLayout+behavior

小红书效果:

简书效果:

demo效果图:

github地址:https://github.com/gm365066360/FlingBehavior

apk下载地址:http://fir.im/zk9g

部分代码:


<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipChildren="false"
            android:fitsSystemWindows="true"
            app:layout_behavior="com.gaom.flingbehaviordemo.FlingBehavior">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="true"
                android:fitsSystemWindows="true"
                app:contentScrim="#ff4444"
                app:expandedTitleMarginEnd="60dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll"
                app:statusBarScrim="#feaa33"
                app:toolbarId="@+id/toolbar">

                
                <ImageView
                    android:id="@+id/main.backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/me"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:layout_gravity="center"
                    android:clipChildren="false"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    app:layout_collapseMode="parallax">

                    <LinearLayout
                        android:id="@+id/collapsing_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:fitsSystemWindows="true"
                        android:orientation="vertical"
                        app:layout_collapseParallaxMultiplier="0.7"
                        app:layout_scrollFlags="scroll"
                        app:titleEnabled="false">

                        <LinearLayout
                            android:id="@+id/header_view"
                            android:layout_width="match_parent"
                            android:layout_height="100dp"
                            android:animateLayoutChanges="true"
                            android:background="#77333333"
                            android:fitsSystemWindows="true"
                            android:orientation="vertical"
                            app:layout_collapseParallaxMultiplier="0.7">

                            <TextView
                                android:layout_width="match_parent"

                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:gravity="center"
                                android:text="我是视察的View,哦,背景也是,可以设置比例哦~"
                                android:textColor="#fff" />

                        LinearLayout>

                        <FrameLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:visibility="invisible" />
                    LinearLayout>

                android.support.v7.widget.Toolbar>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="30dp"
                    android:layout_marginTop="?attr/actionBarSize"
                    android:background="@android:color/transparent"
                    android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|start|end"
                    android:orientation="vertical"
                    app:layout_collapseMode="pin">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:background="#77333333"
                        android:gravity="center"
                        android:text="我是固定不动被顶着走的view"
                        android:textColor="#000" />

                LinearLayout>
            android.support.design.widget.CollapsingToolbarLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_gravity="top|bottom|center_vertical|fill_vertical|center|fill"

                android:orientation="vertical">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tablayout_user"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="@color/colorPrimary"
                    app:layout_scrollFlags="enterAlways"
                    app:tabGravity="center"
                    app:tabIndicatorColor="@android:color/white"
                    app:tabIndicatorHeight="2dp"
                    app:tabMaxWidth="0dp"
                    app:tabMode="fixed"
                    app:tabPaddingEnd="20dp"
                    app:tabPaddingStart="20dp"
                    app:tabSelectedTextColor="@android:color/black"
                    app:tabTextColor="@android:color/white" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/colorPrimary" />

            LinearLayout>
        android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    android.support.design.widget.CoordinatorLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="top|bottom|center_vertical|fill_vertical|center|fill"
        android:background="@android:color/transparent"
        android:orientation="vertical">

    LinearLayout>
RelativeLayout>
package com.gaom.flingbehaviordemo;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.AppBarLayout.Behavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

public class FlingBehavior extends Behavior {
    private boolean a;

    public FlingBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
        if ((velocityY > 0.0f && !this.a) || (velocityY < 0.0f && this.a)) {
            velocityY *= -1.0f;
        }
        if ((target instanceof RecyclerView) && velocityY < 0.0f) {
            RecyclerView recyclerView = (RecyclerView) target;
            if (recyclerView.getChildAdapterPosition(recyclerView.getChildAt(0)) > 3) {
                consumed = true;
            } else {
                consumed = false;
            }
        }
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        this.a = dy > 0;
    }
}

你可能感兴趣的:(Android-github)