短视频商城源码详情页嵌套滑动效果实现

短视频商城源码详情页嵌套滑动效果实现的相关代码
对于BottomSheetBehavior后续会和Behavior单独写文章说明下使用和自定义相关的。

复制代码
<?xml version="1.0" encoding="utf-8"?>
<org.fireking.laboratory.qingtingfm.detail_ios.widget.BottomSheetRootFrameLayout 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"
tools:context="org.fireking.laboratory.qingtingfm.detail_ios.QtDetailsIosActivity">

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/flAudioInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sp_audio_bg"
android:orientation="vertical">

<View
android:id="@+id/vStatusBar"
android:layout_width="match_parent"
android:layout_height="25dp" />

<org.fireking.laboratory.qingtingfm.detail_ios.widget.QtFmAppbar
android:id="@+id/fmAppbar"
android:layout_width="match_parent"
android:layout_height="44dp" />

<org.fireking.laboratory.qingtingfm.detail_ios.widget.QtFmContent
android:id="@+id/qtFmContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:visibility="gone">

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/bottomSheetLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sp_round_12"
android:orientation="vertical"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<net.lucode.hackware.magicindicator.MagicIndicator
android:id="@+id/magicIndicator"
android:layout_width="match_parent"
android:layout_height="50dp" />

<View
android:layout_width="match_parent"
android:layout_height="0.7dp"
android:background="#EEEEEE" />

<androidx.viewpager.widget.ViewPager
android:id="@+id/vPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</org.fireking.laboratory.qingtingfm.detail_ios.widget.BottomSheetRootFrameLayout>
复制代码

 

BottomSheetRootFrameLayout 中主要做的事情就是用来计算BottomSheetBehavior默认展开状态下最大应该展示多少距离,这个距离是不能写死的,因为需要根据请求到的内容来动态计算,所以这里重写了onMeasure,来根据内容变化动态计算出来一个实际的高度并设置给BottomSheetBehavior的peekHeight方法。

复制代码
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
bottomSheetLayout?.also {
val newBottomSheetHeight =
(measuredHeight - dip2px(44F) - StatusBarUtil.getStatusBarHeight(context)).toInt()
if (bottomSheetHeight != newBottomSheetHeight) {
it.layoutParams.height = newBottomSheetHeight
bottomSheetHeight = newBottomSheetHeight
}
val newPeekHeight =
(measuredHeight - qtFmContent!!.measuredHeight - dip2px(44F) - StatusBarUtil.getStatusBarHeight(
context
)).toInt()
if (currentPeekHeight != newPeekHeight && !isFinal) {
val mBottomSheetBehavior = BottomSheetBehavior.from(it)
mBottomSheetBehavior.peekHeight = newPeekHeight
currentPeekHeight = newPeekHeight
//设置高度后,进行二次计算,确保布局同步
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
isFinal = true
}
}
}
复制代码

 

完成上面的计算设置之后,你会发现BottomSheetBehavior在没有填充SmartRefreshLayout和RecyclerView的情况下,可以正常使用了,且基本满足折叠需求,而且折叠展示的高度都是满足实际需求的。
标题栏的展示隐藏控制,可以直接使用`BottomSheetBehavior#onStateChanged进行处理

复制代码
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout)
bottomSheetBehavior.setBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
fmAppbar.showTitle()
} else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
fmAppbar.hideTitle()
}
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {

}
})
复制代码

 

 
以上就是 短视频商城源码详情页嵌套滑动效果实现的相关代码,更多内容欢迎关注之后的文章

posted @   云豹科技-苏凌霄  阅读(96)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示