android 在片段中实现bottomSheet行为

vjhs03f7  于 12个月前  发布在  Android
关注(0)|答案(2)|浏览(166)

我试图实现底部工作表行为的片段.这两点是需要implents i)调用一个片段到另一个按钮时被点击. ii)应用底部工作表行为的片段B,所以当我向下拖动片段可以隐藏/解散.目前我有两个片段A,B.我调用片段B在片段A的容器当按钮被点击.
但是当我试图实现第二点时,我得到了错误。在FrgmentB上,我只扩展了Fragment而不是Fragmentdialog。主要原因是我无法访问后台UI。

u4vypkhs

u4vypkhs1#

我以前也有同样的问题,我所做的是将片段更改为BottomSheetDialogFragment并做了一些更改。

piah890a

piah890a2#

我通过扩展DialogFragment()创建了一个服装BottomeSheetFragement。由于Jetpack Compose和BottomSheetDialogFragment()的一些问题,我没有像前面的评论中提到的那样使用BottomSheetDialogFragment。但是如果你根本没有使用Jetpack Compose或者没有遇到任何问题,我绝对推荐在我的解决方案之前使用BottomSheetDialogFragment()

open class BottomSheetFragment : DialogFragment() {

    private lateinit var binding: BottomSheetFragmentBinding

    private var initialTouchY: Float = 0f
    private var currentOffset: Int = 0
    private var prevY: Int = 0
    private var initialHeight: Int = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = BottomSheetFragmentBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)

        view.post {
            initialHeight = view.height
        }

        dialog?.window?.decorView?.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        initialTouchY = event.rawY
                        return true
                    }

                    MotionEvent.ACTION_MOVE -> {
                        dialog?.window?.attributes?.gravity = Gravity.BOTTOM
                        val deltaY = (event.rawY - initialTouchY).toInt()
                        val offsetBy = deltaY - prevY

                        dialog?.window?.attributes?.dimAmount = (1 - (currentOffset.toFloat().div(initialHeight.toFloat()))) * 0.6f

                        dialog?.window?.setLayout(
                            WindowManager.LayoutParams.MATCH_PARENT,
                            initialHeight
                        )

                        view.offsetTopAndBottom(offsetBy)
                        currentOffset += offsetBy
                        prevY = deltaY

                        return true
                    }

                    MotionEvent.ACTION_UP -> {
                        if (currentOffset < initialHeight * 0.4) {
                            val resetValue = -currentOffset

                            view.offsetTopAndBottom(resetValue)
                            dialog?.window?.attributes?.dimAmount = 0.6f
                            dialog?.window?.setLayout(
                                WindowManager.LayoutParams.MATCH_PARENT,
                                initialHeight
                            )
                            currentOffset = 0
                            prevY = 0
                        } else {
                            dismiss()
                        }
                        v?.performClick()
                        return true
                    }
                }
                return false
            }
        })
    }

    override fun onStart() {
        super.onStart()
        val window: Window? = dialog?.window
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        window?.setGravity(Gravity.BOTTOM)
    }
}

个字符

相关问题