可以使用 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的图片大小修改请看上一篇文章