BottomNavigationView控制fragment跳轉

實現BottomNavigationView控制fragment跳轉比較簡單,直接在相關路徑下右鍵/new/Activity/Bottom Navigation Activity,便會自動生成相關內容,然後根據需求修改即可。

一、生成相關文件

1)Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

app:menu="@menu/bottom_nav_menu" 關聯導航按鈕菜單內容;android:name=“androidx.navigation.fragment.NavHostFragment” 設置Fragment容器;app:defaultNavHost=“true” 把系統後退按鈕連接到NavHostFragment;
app:navGraph="@navigation/mobile_navigation" 將NavHostFragment和導航圖關聯起來。

2)menu/bottom_nav_menu
設置導航按鈕菜單內容,id、icon、title

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

3)navigation/mobile_navigation
navigation資源文件,綁定對應的Fragment
app:startDestination="@+id/navigation_home" 設置開始加載的第一個Fragment
注意:fragment裏面的id一定要跟menu資源文件item的id相一致,否則會出現BottomNavigationView按鈕無法點擊的問題

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.huson.navigationtest.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.huson.navigationtest.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.huson.navigationtest.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

4)MainActivity實現功能

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        //獲取ActionBar設置,
        // 通過NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);設置ActionBar的title
        //如果不顯示ActionBar可以注析掉
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        //設置ActionBar的title,如果主題不含有ActionBar會報錯
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        //設置導航控制
        NavigationUI.setupWithNavController(navView, navController);
    }

}

二、其他相擴展

1)BottomNavigationView的item超過三個title不顯示
這個問題解決比較簡單

navView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);

2)需要在BottomNavigationView添加小紅點

//獲取item視圖,然後再給視圖添加上對應的佈局內容
        //0代表是在第一個item上面添加小紅點
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) navView.getChildAt(0);
        View tab = menuView.getChildAt(0);
        BottomNavigationItemView itemView = (BottomNavigationItemView) tab;
        View badge = LayoutInflater.from(this).inflate(R.layout.view_red_tag, menuView, false);
        int dp8 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics());
        BottomNavigationItemView.LayoutParams params = new BottomNavigationItemView.LayoutParams(dp8, dp8);
        params.gravity = Gravity.CENTER_HORIZONTAL;
        params.leftMargin = dp8;
        params.topMargin = 0;
        itemView.addView(badge, params);
        TextView tvBadgeCount = badge.findViewById(R.id.tv_tag_num);
        RelativeLayout rlBadge = badge.findViewById(R.id.rl_red_tag);
        rlBadge.setGravity(Gravity.CENTER_HORIZONTAL);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章