ScrollView滑动—仿微博主页标题栏渐变悬浮及Fragment实现多个内容页面切换
作为一名热爱学习的Android开发工程si,刷微博的时候居然还想着技术呢,觉得自己也是够够了........哈哈哈
进入今天的正题,微博主页大家肯定是看过的,先看一下微博的效果。
(小提示:该Demo是采用kotlin语言编写的,需要配置Kotlin开发环境哦!)
微博的效果大家都看到了,先看看这标题栏悬停的效果。实现方式很多种,我的思路很简单:顶部有一个默认隐藏的标题栏在上面,然后通过计算ScrollView向上滑动的距离,动态控制头部标题导航栏的显示隐藏。
简单分析一下页面布局的结构:
页面布局代码:
<?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)) } // } })
最后实现的效果:
注意注意注意了:如果使用原生ScrollView,会报如下的警告,如果你是用API大于等于23(Android6.0)的手机测试,不会有什么问题,程序正常运行。但是要是低于这个版本的手机,就会导致奔溃,报 java.lang.NoClassDefFoundError:
源码下载请戳: https://github.com/zj593743143/WeiboDetail_Demo
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
大牛直播SDK: 如何实现简单粗暴靠谱的直播抓娃娃方案
市面上的娃娃机方案五花八门,daniulive认为最简单靠谱粗暴性价比的方案如下: RTMP摄像机–>CDN–>daniulive player(专门针对直播娃娃机开通了“超低延迟”模式,公网实际延迟在200~400毫秒). 娃娃机抓取过程录制方案: 行业内首屈一指的录制方案,利用大牛直播播放端SDK,支持两个摄像头切换过程中(SmartPlayerSwitchUrl)录制到同一个MP4文件,便于用户分享抓取过程或进行申诉。 娃娃机视频管理方案: 利用大牛直播的Windows播放器,同时多窗口播放rtmp摄像机数据,采用轮询播放的方式,有设备发生重连或断线之类,实时统计或者告警,保证前端rtmp摄像机的正常运转。 相关播放器下载: Windows平台下载 Android平台下载 iOS平台下载 比快更快,效果轻松PK市面上“全球第一、国际领先”的各类技术方案。 大牛直播精英群: 294891451 大牛直播技术交流群: 499687479 最近接到最大的质疑是:为什么只有大牛直播推荐这种rtmp摄像头方案? 回答:我们不是做云服务的,通俗点说,我们不是靠带宽盈利的,我们期望...
- 下一篇
Android可折叠收缩伸展的Expandable分组RecyclerView:模型抽象和封装(二)
Android可折叠收缩伸展的Expandable分组RecyclerView:模型抽象和封装(二) 如今Android这种可收缩伸展的Expandable RecyclerView是如此常用,在附录1的基础上,我重新把模型进行了抽象和封装,设计了一套新的架构和简洁的使用方式支持这种功能。重点就是ExpandableItemAdapter类,可直接把RecyclerView的Adapter直接继承ExpandableItemAdapter,然后在建立数据模型时候,把具有“组”意义的数据添加子Item时候,调用addSubItem,这样该RecyclerView就具有伸缩和展开的功能。 每一个Item内部存储了一个isExpand值,isExpand值决定在ExpandableItemAdapter初始后每一组是否伸展和收缩。isExpand为true时候,展开,为false时候,收起。 以下是全部代码。 Item.java: package app.zhangphil.exp; import java.util.ArrayList; import java.util.UUID; pub...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Hadoop3单机部署,实现最简伪集群
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8编译安装MySQL8.0.19