CoordinatorLayout+AppBarLayout+appbar_scrolling_view_behavior

本文详细介绍了CoordinatorLayout、AppBarLayout及appbar_scrolling_view_behavior的联合使用方法,通过实例展示了如何实现复杂的嵌套滑动效果,包括头部图片跟随滚动隐藏等特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

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);
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值