2021/11/10日高德的首页效果是这样的
然后我的效果
关键代码
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
//3 STATE_EXPANDED 4 STATE_COLLAPSED 5 STATE_HIDDEN
Log.d("Swt", "newState" + newState)
if (newState == BottomSheetBehavior.STATE_SETTLING) {
if (lastNewState == BottomSheetBehavior.STATE_EXPANDED) {
if (mSlideOffset < 1f) {
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
lastNewState = BottomSheetBehavior.STATE_HALF_EXPANDED
}
return
}
if (lastNewState == BottomSheetBehavior.STATE_HALF_EXPANDED) {
if (mSlideOffset > halfExpandedRatio) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
lastNewState = BottomSheetBehavior.STATE_EXPANDED
}
if (mSlideOffset < halfExpandedRatio) {
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
lastNewState = BottomSheetBehavior.STATE_COLLAPSED
}
return
}
if (lastNewState == BottomSheetBehavior.STATE_COLLAPSED && mSlideOffset > peekRatio) {
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
lastNewState = BottomSheetBehavior.STATE_HALF_EXPANDED
return
}
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
Log.d("Swt", "slideOffset" + slideOffset)
mSlideOffset = slideOffset
}
})
代码地址
https://github.com/BigSweet/GDHome
有用的话点个start,自己研究写的,网上一般都是自定义的behavior,我发现bottomsheet自己就有三级状态直接用一个变量控制实现了