仿抖音点击评论弹出评论窗口 联动视频缩放效果

2 天前
 rcj6056

各位安卓大佬 求助下

仿照个效果 点击页面按钮弹出一个 bottomSheetDialog dialog.show 的时候 为什么没有走 onSlide 回调呢 导致我在 dialog 弹出来的时候没拿到 slideOffset 偏移量 没法设置跟布局的缩放动画

但是在拖动的时候确实是回调了 onSlide 这是为啥

val bottomSheetDialog = BottomSheetDialog(requireActivity()) val inflate = layoutInflater.inflate(R.layout.bottom_sheet_layout, null) var gridView = inflate.findViewById<GridView>(R.id.gv) gridView.adapter = GridAdapter(requireActivity())

    bottomSheetDialog.setContentView(inflate)
    val behavior = BottomSheetBehavior.from(inflate.parent as ViewGroup)
    behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {
            when (newState) {
                BottomSheetBehavior.STATE_EXPANDED -> Log.i(TAG, " onStateChanged,  STATE_EXPANDED")
                BottomSheetBehavior.STATE_COLLAPSED -> {
                    Log.i(TAG, ",onStateChanged  STATE_COLLAPSED")
                }

                BottomSheetBehavior.STATE_HIDDEN -> Log.i(TAG, ",  onStateChanged STATE_HIDDEN")
            }
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
            Log.i(TAG, ",  onSlide STATE_EXPANDED slideOffset :${slideOffset}")
            slideOffsetListener?.onSlideOffsetChanged(slideOffset)
        }
    })
    bottomSheetDialog.show()
    
    
    

后续拖动的时候回调了 onSlide 拿到了偏移量 很奇怪

772 次点击
所在节点    Android
3 条回复
rcj6056
2 天前
能提供解决方案 有偿一点~
sherlockGou
2 天前
onSlide 本身就是用户主动拖动才会回调的,直接调用 show 或者 expand 是不会触发这个回调的。要实现类似拖动的效果,可以手动模拟拖动来计算 offset ,并设置视频缩放,参考代码如下:

val bottomSheetDialog = BottomSheetDialog(requireActivity())
val contentView = layoutInflater.inflate(R.layout.bottom_sheet_layout, null)
bottomSheetDialog.setContentView(contentView)

// 获取 bottomSheet 的 View
bottomSheetDialog.setOnShowListener { dialog ->
val bottomSheet = (dialog as BottomSheetDialog)
.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)

bottomSheet?.post {
val behavior = BottomSheetBehavior.from(bottomSheet)

// 添加回调监听
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
Log.i(TAG, "onStateChanged: $newState")
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {
Log.i(TAG, "onSlide: $slideOffset")
slideOffsetListener?.onSlideOffsetChanged(slideOffset)
}
})

// 模拟 onSlide 回调
val parentHeight = (bottomSheet.parent as View).height.toFloat()
val currentHeight = bottomSheet.height.toFloat()

// 注意:Google 官方行为可能不一定是线性计算 slideOffset ,这里是近似值
val simulatedOffset = (currentHeight / parentHeight).coerceIn(0f, 1f)

Log.i(TAG, "模拟 onSlide, 当前高度: $currentHeight, 总高度: $parentHeight, offset: $simulatedOffset")
slideOffsetListener?.onSlideOffsetChanged(simulatedOffset)
}
}

bottomSheetDialog.show()
sherlockGou
2 天前
我最近也做了这个功能,但是我没有使用 BTD ,是自己写的布局+动画实现的,更自由一些。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://yangjunhui.monster/t/1130957

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX