Android实现滑动悬浮置顶(一)采用Material Design新控件实现

本文介绍了一种基于CoordinatorLayout实现的Material Design滚动效果方案。通过AppBarLayout与NestedScrollView的组合使用,展示了如何设置scrollFlags来控制滚动行为,实现优雅的UI交互体验。

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

我们直接来看布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/top_left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <include layout="@layout/main_search" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/layoutContentd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/main_search"

        android:visibility="visible">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            <!--
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"-->
            <!--<android.support.design.widget.CollapsingToolbarLayout-->
            <!--android:id="@+id/collapsingToolbar"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->
            <!--app:contentScrim="#00000000"-->
            <!--app:layout_scrollFlags="scroll|exitUntilCollapsed"-->
            <!--app:statusBarScrim="#00000000">-->

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:orientation="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_scrollFlags="scroll">

                <com.youth.banner.Banner
                    android:id="@+id/banner"
                    android:layout_width="match_parent"
                    android:layout_height="350px" />

                <include layout="@layout/mian_top_item" />
            </LinearLayout>
<!--</android.support.design.widget.CollapsingToolbarLayout>-->

            <!--悬浮的部分-->
            <LinearLayout
                android:id="@+id/layTabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFE4C4"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="tab1"
                    android:textColor="#333333" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="tab2"
                    android:textColor="#333333" />

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

        <!--主内容-->
        <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:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="@string/text" />

        </android.support.v4.widget.NestedScrollView>


    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>
来看下效果图



注意:

CoordinatorLayout 实现了多种Material Design中提到的滚动效果

响应滚动事件AppBarLayout必须是CoordinatorLayout的直接子View,然后我们需要定义AppBarLayout与滚动视图之间的联系,在任意支持嵌套滚动的view上添加app:layout_behavior,AppBarLayout里面定义的view只要设置了app:layout_scrollFlags属性,必须至少启用scroll这个flag,这样这个view才会滚动出屏幕


源码地址


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

揽m月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值