opt Fragments

androidx
Zhanty 5 years ago
parent 7682edda28
commit 29cd0754d3
  1. 31
      lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt

@ -214,7 +214,7 @@ private class SafelyFragmentTransactionFragmentDelegate : FragmentDelegate<Fragm
} }
override fun onResume() { override fun onResume() {
if (!mPendingTransactions.isEmpty()) { if (mPendingTransactions.isNotEmpty()) {
mPendingTransactions.forEach { it.commit() } mPendingTransactions.forEach { it.commit() }
mPendingTransactions.clear() mPendingTransactions.clear()
} }
@ -243,7 +243,7 @@ class EnhanceFragmentTransaction constructor(
val nonnullTag = (tag ?: fragment.javaClassName()) val nonnullTag = (tag ?: fragment.javaClassName())
addToBackStack(nonnullTag) addToBackStack(nonnullTag)
//add //add
fragmentTransaction.add(containerId.confirmId(), fragment, nonnullTag) fragmentTransaction.add(confirmLayoutId(containerId), fragment, nonnullTag)
if (transition) { if (transition) {
//set a transition //set a transition
setTransitionOpen() setTransitionOpen()
@ -252,8 +252,7 @@ class EnhanceFragmentTransaction constructor(
} }
/** /**
* replace 方式把 [fragment] 添加到回退栈中 * replace 方式把 [fragment] 添加到回退栈中如果 [containerId]==0则使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id
* 如果 [containerId]==0则使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id
* 如果 [tag] ==null 则使用 fragment 对应 class 的全限定类名 * 如果 [tag] ==null 则使用 fragment 对应 class 的全限定类名
*/ */
fun replaceWithStack(containerId: Int = 0, fragment: Fragment, tag: String? = null, transition: Boolean = true): EnhanceFragmentTransaction { fun replaceWithStack(containerId: Int = 0, fragment: Fragment, tag: String? = null, transition: Boolean = true): EnhanceFragmentTransaction {
@ -261,7 +260,7 @@ class EnhanceFragmentTransaction constructor(
val nonnullTag = (tag ?: fragment.javaClassName()) val nonnullTag = (tag ?: fragment.javaClassName())
addToBackStack(nonnullTag) addToBackStack(nonnullTag)
//add //add
fragmentTransaction.replace(containerId.confirmId(), fragment, nonnullTag) fragmentTransaction.replace(confirmLayoutId(containerId), fragment, nonnullTag)
//set a transition //set a transition
if (transition) { if (transition) {
setTransitionOpen() setTransitionOpen()
@ -269,18 +268,16 @@ class EnhanceFragmentTransaction constructor(
return this return this
} }
private fun Int.confirmId(): Int { private fun confirmLayoutId(layoutId: Int): Int {
return if (this == 0) { return if (layoutId == 0) {
FragmentConfig.defaultContainerId() FragmentConfig.defaultContainerId()
} else { } else {
this layoutId
} }
} }
/** /**
* 添加 [fragment] * 添加 [fragment]默认使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id如果 [tag] 为null则使用 [fragment] 的全限定类
* 默认使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id
* 如果 [tag] 为null则使用 [fragment] 的全限定类名*
*/ */
fun addWithDefaultContainer(fragment: Fragment, tag: String? = null): FragmentTransaction { fun addWithDefaultContainer(fragment: Fragment, tag: String? = null): FragmentTransaction {
val nonnullTag = (tag ?: fragment.javaClassName()) val nonnullTag = (tag ?: fragment.javaClassName())
@ -288,9 +285,7 @@ class EnhanceFragmentTransaction constructor(
} }
/** /**
* 替换为 [fragment] * 替换为 [fragment]id 使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id如果 [tag] 为null则使用 [fragment] 的全限定类名
* id 使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id
* 如果 [tag] 为null则使用 [fragment] 的全限定类名
*/ */
fun replaceWithDefaultContainer(fragment: Fragment, tag: String? = null, transition: Boolean = true): FragmentTransaction { fun replaceWithDefaultContainer(fragment: Fragment, tag: String? = null, transition: Boolean = true): FragmentTransaction {
val nonnullTag = (tag ?: fragment.javaClassName()) val nonnullTag = (tag ?: fragment.javaClassName())
@ -301,7 +296,7 @@ class EnhanceFragmentTransaction constructor(
} }
/**隐藏所有的 fragment */ /**隐藏所有的 fragment */
private fun hideOtherFragments() { private fun hideFragments() {
for (fragment in fragmentManager.fragments) { for (fragment in fragmentManager.fragments) {
if (fragment != null && fragment.isVisible) { if (fragment != null && fragment.isVisible) {
fragmentTransaction.hide(fragment) fragmentTransaction.hide(fragment)
@ -317,15 +312,15 @@ class EnhanceFragmentTransaction constructor(
} }
fun setTransitionOpen(): FragmentTransaction { fun setTransitionOpen(): FragmentTransaction {
return fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_OPEN)
} }
fun setTransitionClose(): FragmentTransaction { fun setTransitionClose(): FragmentTransaction {
return fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE) return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_CLOSE)
} }
fun setTransitionFade(): FragmentTransaction { fun setTransitionFade(): FragmentTransaction {
return fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) return fragmentTransaction.setTransition(TRANSIT_FRAGMENT_FADE)
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------

Loading…
Cancel
Save