CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+ImmersionBar实现吸顶悬浮,状态栏沉浸式,顶部图片拉伸效果(二)

话不多说直接上图:
在这里插入图片描述

第一篇的基础上进行了拓展,整体下拉时,顶部图片会有拉伸回弹效果。

核心主要是重写了AppBarLayout使用的layout_behavior,顶部折叠的布局。Activity中只改动了一处,解决整体滑动时不流畅问题,Viewpager嵌套Fragment中展示每个item横向的RecycleView设置为setNestedScrollingEnabled(false);

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    android:background="@color/black"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000"
        app:layout_behavior=".feature.mine.widget.AppbarZoomBehavior"
        tools:ignore="MissingConstraints">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="@color/black"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <!--顶部折叠布局-->
            <com.example.gw.xlp.feature.mine.widget.YYImageWrapRelativeLayout
                android:id="@+id/topLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ececec"
                android:tag="scrollTag"
                app:layout_collapseMode="pin"
                app:layout_collapseParallaxMultiplier="0.7">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:orientation="vertical"
                    android:padding="12dp"
                    android:gravity="left|bottom"
                    android:tag="bottomLayoutTag">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="阿伟哈皮"
                        android:textColor="@color/white"
                        android:textSize="@dimen/sp_18"
                        android:textStyle="bold"/>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="bottom">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="1关注"
                            android:textSize="@dimen/sp_14"
                            android:textColor="@color/white"/>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="1被关注"
                            android:layout_marginStart="@dimen/dp_10"
                            android:textSize="@dimen/sp_14"
                            android:textColor="@color/white"/>
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:layout_marginStart="@dimen/dp_10">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="+2"
                                android:layout_marginStart="@dimen/dp_20"
                                android:textColor="@color/red"/>
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="6人气"
                                android:textSize="@dimen/sp_14"
                                android:textColor="@color/white"/>
                        </LinearLayout>
                    </LinearLayout>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="21/170/60"
                        android:textSize="@dimen/sp_14"
                        android:textColor="@color/light_gray"
                        android:layout_marginTop="@dimen/dp_10"
                        android:drawablePadding="@dimen/dp_10"
                        android:drawableLeft="@mipmap/icon_white_user"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="北京"
                        android:textSize="@dimen/sp_14"
                        android:textColor="@color/light_gray"
                        android:layout_marginTop="@dimen/dp_10"
                        android:drawablePadding="@dimen/dp_10"
                        android:drawableLeft="@mipmap/icon_white_location"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="你还没有填写个人介绍,点击添加"
                        android:textSize="@dimen/sp_14"
                        android:textColor="@color/light_gray"
                        android:layout_marginTop="@dimen/dp_10"
                        android:drawablePadding="@dimen/dp_10" />

                </LinearLayout>

            </com.example.gw.xlp.feature.mine.widget.YYImageWrapRelativeLayout>
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_40"
                app:layout_collapseMode="pin">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/dp_30"
                    android:gravity="right">
                    <ImageView
                        android:id="@+id/img_right"
                        android:layout_width="@dimen/dp_24"
                        android:layout_height="@dimen/dp_32"
                        android:layout_gravity="right"
                        android:src="@mipmap/icon_list_setting" />
                </LinearLayout>

            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.flyco.tablayout.CommonTabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_40"
            android:tag="tabTag"
            app:tl_iconVisible="false"
            app:tl_textBold="SELECT"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:tl_indicator_width="@dimen/dp_70"
            app:tl_indicator_color="@color/white"
            app:tl_indicator_height="@dimen/dp_2"
            app:tl_textsize="@dimen/sp_16"
            app:tl_textSelectColor="@color/white"
            app:tl_textUnselectColor="@color/white">
        </com.flyco.tablayout.CommonTabLayout>

    </com.google.android.material.appbar.AppBarLayout>


    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </androidx.viewpager.widget.ViewPager>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

在此很感谢博主迷人的羊驼的支持。重写的布局在他资源可下载,一定要注意布局中tag的设置。

大家喜欢的话可以点赞哦~~~

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
好的,针对你的问题,我可以提供一些思路和参考代码。 首先,实现 Android 中的自定义吸顶,可以采用两种方: 1. 使用固定顶部控件 + ScrollView + ViewPager 的方实现。 这种方比较常见,具体实现思路如下: (1)在布局文件中定义一个固定在顶部的控件,例如 LinearLayout,将其设置为可见性为 gone,即不可见。 (2)在 ScrollView 中添加 ViewPager,将其填充满整个布局,用于滑动切换不同的子页面。 (3)监听 ScrollView 的滑动事件,在滑动到一定位置时,将顶部控件设置为可见,实现吸顶效果。 具体实现代码可以参考以下链接: - https://www.jianshu.com/p/4f28a4d0c3b1 - https://www.cnblogs.com/xiaohuafice/p/11050662.html 2. 使用 CoordinatorLayout + AppBarLayout 实现。 这种方相对来说比较简单,具体实现思路如下: (1)在布局文件中使用 CoordinatorLayout 作为根布局,并添加一个 AppBarLayout 作为子布局。 (2)在 AppBarLayout 中添加一个 Toolbar 控件作为顶部的固定控件,将其设置为可见性为 gone。 (3)在子页面中,使用 NestedScrollView 作为滑动的容器,并将其放在 AppBarLayout 的下面。 (4)监听 NestedScrollView 的滑动事件,在滑动到一定位置时,将 Toolbar 设置为可见,实现吸顶效果。 具体实现代码可以参考以下链接: - https://www.jianshu.com/p/5d0f7e7e7c97 - https://www.jianshu.com/p/5d0f7e7e7c97 希望以上内容能够帮助到你,有什么问题可以再和我交流哦!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

成功之路必定艰辛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值