@Route(path = Constant.PAGE_HOME)
class HomeActivity : BaseActivity() {
private lateinit var mRgHome: RadioGroup
private var mCurrentFragment = Fragment()
private lateinit var mFragTransition: FragmentTransaction
override fun getContentViewId(): Int {
return R.layout.activity_home
}
override fun onViewCreated(savedInstanceState: Bundle?) {
super.onViewCreated(savedInstanceState)
CommonTaskData.loadData()
mRgHome = findViewById(R.id.rg_home)
val workFragment = WorkFragment.newInstance()
val studyFragment = StudyFragment.newInstance()
val notificationFragment = NotificationFragment.newInstance()
val mineFragment = MineFragment.newInstance()
mRgHome.setOnCheckedChangeListener { _, checkedId ->
val mCheckFragment: Fragment? = when (checkedId) {
R.id.rb_home_study -> studyFragment
R.id.rb_home_notification -> notificationFragment
R.id.rb_home_mine -> mineFragment
R.id.rb_home_work -> workFragment
else -> null
}
mCheckFragment?.let {
switchFragment(it)
}
}
//默认
mRgHome.check(R.id.rb_home_work)
switchFragment(workFragment)
}
private fun switchFragment(to: Fragment) {
mFragTransition = supportFragmentManager.beginTransaction()
if (mCurrentFragment != to) {
mFragTransition.hide(mCurrentFragment)
mCurrentFragment = to
if (!to.isAdded) {
mFragTransition.add(R.id.fl_home, to).show(to).commitAllowingStateLoss()
} else {
mFragTransition.show(to).commitAllowingStateLoss()
}
}
}
}
fragment add hide show
最新推荐文章于 2020-11-03 19:01:39 发布