作為一名熱愛學習的Android開發工程si,刷微網誌的時候居然還想着技術呢,覺得自己也是夠夠了........哈哈哈
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuYjYhRjZ3ETMmJGMxEjYlF2YhFjN0UTM3MWYhRjMkVmYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
image.png
進入今天的正題,微網誌首頁大家肯定是看過的,先看一下微網誌的效果。
(小提示:該Demo是采用kotlin語言編寫的,需要配置Kotlin開發環境哦!)
wb.gif
微網誌的效果大家都看到了,先看看這标題欄懸停的效果。實作方式很多種,我的思路很簡單:頂部有一個預設隐藏的标題欄在上面,然後通過計算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))
}
//
}
})
最後實作的效果:
re.gif
注意注意注意了:如果使用原生ScrollView,會報如下的警告,如果你是用API大于等于23(Android6.0)的手機測試,不會有什麼問題,程式正常運作。但是要是低于這個版本的手機,就會導緻奔潰,報 java.lang.NoClassDefFoundError:
解決辦法很簡單,就是自定義一個ScrollView,寫一個接口将onScrollChange()暴露出去。
源碼下載下傳請戳:
https://github.com/zj593743143/WeiboDetail_Demo