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

相关文章
|
3月前
|
前端开发
Flutter笔记:光影动画按钮、滚动图标卡片组等
Flutter笔记:光影动画按钮、滚动图标卡片组等
58 0
|
JavaScript Serverless 容器
ue仿携程轮播图效果(滑动轮播,下方高度自适应)
这篇文章主要介绍了vue仿携程轮播图效果(滑动轮播,下方高度自适应),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
|
移动开发
【笔记】一行代码完成——h5页面上滑图片渐隐
一行代码完成——h5页面上滑图片渐隐
145 0
|
架构师 开发者
全屏幻灯控件|学习笔记
快速学习全屏幻灯控件。
55 0
|
定位技术 Android开发
Android仿饿了么地图滑动悬停华丽效果
Android仿饿了么地图滑动悬停华丽效果
345 0
Android仿饿了么地图滑动悬停华丽效果
|
Android开发
【Android视图效果】仿QQ空间滑动改变标题栏颜色
最近在倒腾公司之前的项目,发现之前的界面是个白色标题栏,不是很美观,所以做了些改进。 先看效果图 165815uykp80g8y3goo5vz.gif 简单说下思路,整个布局大体上是ScrollView里面包含了一个ImageView和RecyclerView,所以先得到ImageView的高度,当ScrollView向上滑动时,设置标题栏的背景色、文字颜色,当超过ImageView的高度时,设置其背景为白色,字体为黑色。
1151 0
|
Android开发
Android 优化个人封装仿网易新闻可滑动标题栏 TabLayout (文字或图标)
      小菜在向朋友推荐了自己修改封装的仿网易顶部滑动标题栏 TabSlideLayout 滑动内容可以是文字也可以是网络图标,其原型为 FlycoTabLayout,但是因为年代很久远,小菜当时技术太渣,存在一些小问题,后期做过一些优化,今天趁机会整理一下。
3014 0
|
Android开发 容器
仿网易新闻可滑动标题栏TabLayout(文字或图标)
    近期有需要,要做一个类似于网易新闻首页中的可滑动标题栏 TabLayout,根据大神写的 FlycoTabLayout 改造了一下,可以加载网络图片,主要实现内容如下:     1.
1689 0
|
前端开发 JavaScript
按钮点击效果(水波纹)
近来看到个不错的按钮点击效果,当点击时产生一次水波涟漪效果,挺好玩的,于是简单的实现了下(没考虑低版本浏览器兼容问题) 先看看效果吧,如下图(录制gif软件有点渣,看起来卡卡的...) 这种效果可以由元素内嵌套canves实现,也可以由css3实现。
1338 0