【发布时间】:2018-01-24 14:35:18
【问题描述】:
我已经开始学习 Kotlin,但不知道该语言的所有功能。
该函数用于在FrameLayout中显示片段。逻辑是这样的,第一次它应该总是add()片段,下一次它会replace()。另外在某些情况下我需要使用addToBackStack() 并且在相同的情况下也需要禁用左侧菜单。
fun showFragment(fragment : Fragment,
isReplace: Boolean = true,
backStackTag: String? = null,
isEnabled: Boolean = true)
{
/* Defining fragment transaction */
val fragmentTransaction = supportFragmentManager
.beginTransaction()
/* Select if to replace or add a fragment */
if(isReplace)
fragmentTransaction.replace(R.id.frameLayoutContent, fragment, backStackTag)
else
fragmentTransaction.add(R.id.frameLayoutContent, fragment)
/* Select if to add to back stack */
if(backStackTag != null)
fragmentTransaction.addToBackStack(fragment.javaClass.name)
fragmentTransaction.commit()
enableDrawer(isEnabled)
}
问题:是否有可能对 Kotlin 语言规范相关的函数代码进行一些改进,以使代码更清晰,因为现在函数看起来像一个整体。
【问题讨论】:
标签: android android-fragments kotlin