繁体   English   中英

更改底部工作表内的片段

[英]Change fragment inside bottom sheet

我有两个底片片段,授权和注册。 我可以在另一个顶部打开一个,但它看起来不太好。 我想用一张底纸内的另一个片段替换一个片段。 是否可以?

所以。 为了实现这个想法,我稍微改变了应用程序的结构。 我创建了一个BaseBottomSheet类,在它的布局中有一个片段容器(我设置了一个空的 FrameLayout)。 哪个片段会出现是由bungle中的标签决定的。 此外,如果您需要将一个片段更改为另一个片段内部的片段,例如,通过使用按钮,我创建了parentFragment as BaseBottomSheet...可用的公共片段替换函数parentFragment as BaseBottomSheet...

fun showAuthFragment() {
    val fragmentTransaction = childFragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragmentContainer, AuthView())
    fragmentTransaction.commit()
    expandView()
}

fun showRegistrationFragment() {
    val fragmentTransaction = childFragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragmentContainer, RegistrationView())
    fragmentTransaction.commit()
    expandView()
}

此外,如果您的片段具有不同的大小,则显示时您的片段可能不会完全可见。 我通过创建 bottomSheetBehavior 变量并在 onCreateView 中初始化它来解决这个问题 *(这是必要的,只是调用和设置 BottomSheetBehavior 的值是行不通的)

    private var bottomSheetBehavior: BottomSheetBehavior<View>? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    dialog?.setOnShowListener { dialog ->
        val bottomSheet = (dialog as BottomSheetDialog).findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?

        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet!!)
    }

    return inflater.inflate(R.layout.base_bottom_sheet, container, false)
}

fun expandView() {
    bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM