android fragment 切换时重新createView问题

可以使用 RadioGroup radioButton 解决

这个方法新旧版本都没问题, 改写 FragmentNavigator的方式在比较新的库依赖中,会出现崩溃问题

如下:

java.lang.IllegalArgumentException: The fragment RadomFragment is unknown to the FragmentNavigator. Please use the navigate() function to add fragments to the FragmentNavigator managed FragmentManager.

解决方案(还是老的方式靠谱点):

MainActivity.xml 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        android:layout_above="@+id/nav_view"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:itemRippleColor="@null"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/home_tab"
            style="@style/Custom.TabRadioButton"
            android:checked="true"
            android:text=""
            android:background="@drawable/call_bottom_tab"
            android:drawableTop="@drawable/tab1_selector" />
        <RadioButton
            android:id="@+id/match_tab"
            android:text=""
            android:background="@drawable/call_bottom_tab"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/tab2_selector"
            />
        <RadioButton
            android:id="@+id/rank_tab"
            android:background="@drawable/call_bottom_tab"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/tab3_selector"
            />
    </RadioGroup>


</RelativeLayout>

MainActivity.txt

private var mTabRadioGroup: RadioGroup? = null
private var curFragment:  Fragment? = null
private var homeFragment: Fragment? = null
private var rankFragment: Fragment? = null
 
private var matchFragment: Fragment? = null
private var mFragmentSparseArray: SparseArray<Fragment>? = null

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

initView()

}

@RequiresApi(Build.VERSION_CODES.O)
//切换判断
private fun switchFragment(targetFragment: Fragment): FragmentTransaction? {
    if (curFragment != null && curFragment === targetFragment) return null
    val transaction = supportFragmentManager.beginTransaction()
    if (!targetFragment.isAdded) {
        if (curFragment != null) {
            transaction.hide(curFragment!!)
        }
        transaction.add(R.id.nav_host_fragment_activity_main, targetFragment, targetFragment.javaClass.name)
    } else {
        curFragment?.let {
            transaction.hide(it).show(targetFragment)
            targetFragment.onResume()
        }
    }
    curFragment = targetFragment
    return transaction
}


 
@RequiresApi(Build.VERSION_CODES.O)
private fun initView() {
    mTabRadioGroup = findViewById(R.id.nav_view)

     
    mFragmentSparseArray = SparseArray<Fragment>()
    rankFragment = RankFragment()
    homeFragment = HomeFragment()
 
    matchFragment = MatchFragment()
    mFragmentSparseArray!!.append(R.id.home_tab, homeFragment)
    mFragmentSparseArray!!.append(R.id.match_tab, matchFragment)
    mFragmentSparseArray!!.append(R.id.rank_tab, rankFragment)
  
    mTabRadioGroup!!.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
        val transaction = switchFragment(mFragmentSparseArray!!.get(checkedId))
        transaction?.commit()
    })

    val transaction = switchFragment(mFragmentSparseArray!!.get(R.id.home_tab))
    transaction?.commit()
}

@RequiresApi(Build.VERSION_CODES.O)
override fun onResume() {
    super.onResume()
        curFragment?.onResume()
}

RadioButton的图片大小修改请看上一篇文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值