CoordinatorLayout+AppBarLayout+appbar_scrolling_view_behavior

来源:互联网 发布:星际老男孩淘宝 编辑:程序博客网 时间:2024/06/14 12:35

这里写图片描述

CoordinatorLayout、AppBarLayout和appbar_scrolling_view_behavior,三者必须联合使用
CoordinatorLayout用于协调子布局嵌套滑动(实现了NestedScrollingParent接口)
AppBarLayout是google默认实现的一种配合嵌套滑动的控件,用于在头部显示,必须是CoordinatorLayout的直接子布局
appbar_scrolling_view_behavior:google默认实现的用于配合实现AppBarLayout效果的Behavior

<?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"    tools:context="com.example.apple.nestedscrolldemo.Main2Activity"    tools:showIn="@layout/activity_main2">    <android.support.design.widget.AppBarLayout        android:id="@+id/app_bar"        android:layout_width="match_parent"        android:layout_height="350dp">        <FrameLayout            app:contentScrim="?attr/colorPrimary"            //栅格遮拦效果(必须在AppBarLayout直接子布局中使用)            android:layout_width="match_parent"            android:layout_height="300dp"                                    app:layout_scrollFlags="scroll|enterAlwaysCollapsed"            //scroll表示该部分是要随着滑动的,enterAlwaysCollapsed表示滑动进入进出效果(必须在AppBarLayout直接子布局中使用)          >            <ImageView                android:layout_width="match_parent"                android:layout_height="300dp"                android:background="@color/colorAccent"                android:scaleType="fitXY"                android:src="@mipmap/game"               />            <TextView                android:id="@+id/tv1111"                android:layout_width="match_parent"                android:layout_height="50dp"                android:layout_gravity="center"                android:gravity="center"                android:background="@color/colorAccent"                android:text="ffffffff"                android:textSize="17sp"/>        </FrameLayout>        <TextView        //这是AppBarLayout第二个直接子布局,这里没写         app:layout_scrollFlags属性(或者没设置该属性没填写scroll),表示不参与嵌套滑动,最终会固定显示这一部分            android:id="@+id/tv1"            android:layout_width="match_parent"            android:layout_height="50dp"            android:layout_gravity="center"            android:gravity="center"            android:text="@string/app_name"            android:textSize="17sp"/>    </android.support.design.widget.AppBarLayout>    //注意:必须在AppBarLayout的兄弟布局中使用一个实现了    NestedScrollingChild接口的布局,并在其中加入  app:layout_behavior="@string/appbar_scrolling_view_behavior",所有效果才能实现    <android.support.v4.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_view_behavior">        <TextView            android:id="@+id/tv"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="@string/large_text"            android:textSize="17sp"/>    </android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout>
        final TextView tv = (TextView) findViewById(R.id.tv1111);        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);//        设置竖直偏移量监听,拿到verticalOffset,控制控件的显示        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {            @Override            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {                Log.e("vvv",verticalOffset+"==="+350*5);                float f = Math.abs(verticalOffset*1.0f / 1050);                tv.setTranslationY(verticalOffset);                tv.setAlpha(1-f);            }        });
原创粉丝点击