ScrollView滑动—仿微博主页标题栏渐变悬浮及Fragment实现多个内容页面切换

简介: 作为一名热爱学习的Android开发工程si,刷微博的时候居然还想着技术呢,觉得自己也是够够了........哈哈哈image.png进入今天的正题,微博主页大家肯定是看过的,先看一下微博的效果。

作为一名热爱学习的Android开发工程si,刷微博的时候居然还想着技术呢,觉得自己也是够够了........哈哈哈


img_bed24aac715461acaeb110bf117f4ab6.png
image.png

进入今天的正题,微博主页大家肯定是看过的,先看一下微博的效果。
(小提示:该Demo是采用kotlin语言编写的,需要配置Kotlin开发环境哦!)


img_9dc7b3e8520635f204a7490403a28398.gif
wb.gif

微博的效果大家都看到了,先看看这标题栏悬停的效果。实现方式很多种,我的思路很简单:顶部有一个默认隐藏的标题栏在上面,然后通过计算ScrollView向上滑动的距离,动态控制头部标题导航栏的显示隐藏。
简单分析一下页面布局的结构:


img_c98dbe2815b00236a10f8ed40c743aab.png
image.png

页面布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/iv_img"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:scaleType="fitXY"
                    android:src="@mipmap/bg_wb" />

                <include
                    android:id="@+id/ll_tab"
                    layout="@layout/layout_suspencial_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/iv_img"></include>

                <FrameLayout
                    android:id="@+id/fl_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/ll_tab"></FrameLayout>
            </RelativeLayout>
        </ScrollView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_title_text"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:gravity="center"
                android:text="歌手李健"
                android:textColor="#ffffff"
                android:textSize="18sp" />

            <View
                android:id="@+id/title_divider"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/tv_title_text"
                android:background="#e6e6e6"
                android:visibility="gone"></View>
            <!--悬停导航标题栏-->
            <include
                android:id="@+id/ll_sus_tab"
                layout="@layout/layout_suspencial_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/title_divider"
                android:visibility="invisible"></include>
        </RelativeLayout>

    </FrameLayout>

</RelativeLayout>

设置标题栏导航栏悬停和标题栏渐变的核心代码:给ScrollView设置滑动的监听


        scrollView.setOnScrollViewListener(object : MyScrollView.OnScrollViewListener {
            override fun onScrollChanged(scrollX: Int, scrollY: Int, oldx: Int, oldY: Int) {
                //如果向上滑动的距离>=iv_img.height - tv_title_text.height,隐藏的标题导航栏设置显示
                var distanceScrollY = iv_img.height - tv_title_text.height
                if (scrollY >= distanceScrollY) {
                    ll_sus_tab.visibility = View.VISIBLE
//                    ll_tab.visibility = View.INVISIBLE
                    title_divider.visibility = View.VISIBLE
                } else {
                    ll_sus_tab.visibility = View.INVISIBLE
//                    ll_tab.visibility = View.VISIBLE
                    title_divider.visibility = View.GONE
                }
                //设置标题栏渐变
                if (scrollY <= 0) {
                    //初始位置:未滑动时,设置标题背景透明
                    tv_title_text.setBackgroundColor(Color.TRANSPARENT)
                    tv_title_text.setTextColor(Color.WHITE)
                } else if (scrollY > 0 && scrollY <= distanceScrollY) {
                    var scale: Float = (scrollY.toFloat()) / distanceScrollY
                    var alpha: Float = 255 * scale
                    tv_title_text.setBackgroundColor(Color.argb(alpha.toInt(), 255, 255, 255))
                    tv_title_text.setTextColor(Color.argb(alpha.toInt(), 0, 0, 0))
                } else {
                    tv_title_text.setBackgroundColor(Color.argb(255, 255, 255, 255))
                    tv_title_text.setTextColor(Color.argb(255, 0, 0, 0))
                }
//
            }
        })

最后实现的效果:

img_c34285134c0296750f77bd6f090039e5.gif
re.gif

注意注意注意了:如果使用原生ScrollView,会报如下的警告,如果你是用API大于等于23(Android6.0)的手机测试,不会有什么问题,程序正常运行。但是要是低于这个版本的手机,就会导致奔溃,报 java.lang.NoClassDefFoundError:
img_54c782a36766b29facab3fa2b9073e09.png
image.png
解决办法很简单,就是自定义一个ScrollView,写一个接口将onScrollChange()暴露出去。
源码下载请戳: https://github.com/zj593743143/WeiboDetail_Demo

相关文章
|
算法 搜索推荐 Android开发
android的A/B到底是什么?OTA升级又是什么?
android的A/B到底是什么?OTA升级又是什么?
887 0
|
消息中间件 人工智能 运维
左手医生:医疗 AI 企业的云原生提效降本之路
通过使用阿里云云原生等产品,左手医生项目的上线时间缩短了 67%,运维效率提升 70% 左右,消息处理的效率也提升了 80% 左右。
757 96
|
前端开发 安全 测试技术
前端组件化有什么优势?
【10月更文挑战第4天】
444 56
|
存储 Java Android开发
Android|记一个导致 logback 无法输出日志的问题
在给一个 Android 项目添加 logback 日志框架时,遇到一个导致无法正常输出日志的问题,这里记录一下。
307 2
|
Java Android开发 UED
🧠Android多线程与异步编程实战!告别卡顿,让应用响应如丝般顺滑!🧵
在Android开发中,为应对复杂应用场景和繁重计算任务,多线程与异步编程成为保证UI流畅性的关键。本文将介绍Android中的多线程基础,包括Thread、Handler、Looper、AsyncTask及ExecutorService等,并通过示例代码展示其实用性。AsyncTask适用于简单后台操作,而ExecutorService则能更好地管理复杂并发任务。合理运用这些技术,可显著提升应用性能和用户体验,避免内存泄漏和线程安全问题,确保UI更新顺畅。
468 5
|
存储 前端开发 安全
上门服务家政系统开发技术规则
上门服务家政系统的开发涵盖市场调研、需求分析、功能规划、系统设计与开发实施等多个阶段。首先,通过调研明确市场需求与用户期望;其次,规划服务预约、支付、评价等功能;接着设计前端、后台及数据库;最后,进行系统开发、测试与优化,确保稳定可靠。
|
Android开发 iOS开发 容器
Android Studio App开发入门之选择按钮的讲解及使用(包括复选框,开关按钮,单选按钮,附源码)
Android Studio App开发入门之选择按钮的讲解及使用(包括复选框,开关按钮,单选按钮,附源码)
617 0
|
SQL 消息中间件 缓存
回滚机制有多少种?它们的实现原理是什么?你确定都知道?
回滚是指当程序或数据出错时,将程序或数据恢复到最近的一个正确版本的行为。最常见的如事务回滚、代码库回滚、部署版本回滚、数据版本回滚、静态资源版本回滚等。通过回滚机制可保证系统在某些场景下的高可用。
|
Android开发
Android 按钮实现按压水波纹效果
Android 按钮实现按压水波纹效果
628 0
Android 按钮实现按压水波纹效果
|
机器学习/深度学习 弹性计算 编解码
阿里云ECS服务器架构GPU/FPGA/ASIC详细说明
阿里云ECS服务器架构GPU/FPGA/ASIC详细说明,阿里云服务器架构有什么区别?X86计算、ARM计算、GPU/FPGA/ASIC、弹性裸金属服务器、超级计算集群有什么区别?阿里云服务器网分享云服务器ECS架构详细说明
577 0