kotlin扩展函数

kotlin 扩展函数

https://www.kotlincn.net/docs/reference/extensions.html

扩展函数在android中的应用

fun AppCompatActivity.replaceFragmentInActivity(fragment: Fragment, @IdRes frameId: Int) {
    supportFragmentManager.transact {
        replace(frameId, fragment)
    }
}

fun AppCompatActivity.addFragmentToActivity(fragment: Fragment, tag: String) {
    supportFragmentManager.transact {
        add(fragment, tag)
    }
}

private inline fun FragmentManager.transact(action: FragmentTransaction.() -> Unit) {
    beginTransaction().apply {
        action()
    }.commit()
}
posted @ 2018-07-23 12:04  戴杭林  阅读(877)  评论(0编辑  收藏  举报