V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
rcj6056
V2EX  ›  Android

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

  •  
  •   rcj6056 · May 11, 2025 · 3516 views
    This topic created in 355 days ago, the information mentioned may be changed or developed.

    各位安卓大佬 求助下

    仿照个效果 点击页面按钮弹出一个 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 拿到了偏移量 很奇怪

    3 replies    2025-05-11 18:23:24 +08:00
    rcj6056
        1
    rcj6056  
    OP
       May 11, 2025
    能提供解决方案 有偿一点~
    sherlockGou
        2
    sherlockGou  
       May 11, 2025
    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
        3
    sherlockGou  
       May 11, 2025
    我最近也做了这个功能,但是我没有使用 BTD ,是自己写的布局+动画实现的,更自由一些。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2409 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:35 · PVG 18:35 · LAX 03:35 · JFK 06:35
    ♥ Do have faith in what you're doing.