iT邦幫忙

2022 iThome 鐵人賽

DAY 25
0
Mobile Development

Android Studio - 30天菜鳥筆記系列 第 25

Android Studio菜鳥筆記 - Day25 - BottomNavigationView + Fragment

  • 分享至 

  • xImage
  •  

BottomNavigationView與Fragment結合
點擊不同Item顯示不同Fragment

首先先創建4個Fragment

package com.example.bottomnavigtionview_Fragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;


import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BlankFragment blankFragment ;
    private BlankFragment2 blankFragment2;
    private BlankFragment3 blankFragment3;
    private BlankFragment4 blankFragment4;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    BottomNavigationView nav;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nav = findViewById(R.id.navigation);
        nav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                switch (id) {
                    case R.id.nav1:
                        blankFragment = new BlankFragment();
                        fragmentTransaction.replace(R.id.frame, blankFragment);
                        fragmentTransaction.commit();
                        return true;
                    case R.id.nav2:
                        blankFragment2 = new BlankFragment2();
                        //fragment覆蓋(replace),使用blankFragment2的class
                        fragmentTransaction.replace(R.id.frame, blankFragment2);
                        fragmentTransaction.commit();
                        return true;
                    case R.id.nav3:
                        blankFragment3 = new BlankFragment3();
                        fragmentTransaction.replace(R.id.frame, blankFragment3);
                        fragmentTransaction.commit();
                        return true;
                    case R.id.nav4:
                        blankFragment4 = new BlankFragment4();
                        fragmentTransaction.replace(R.id.frame, blankFragment4);
                        fragmentTransaction.commit();
                        return true;
                }
                return false;
            }
        });
    }
}
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="com.example.bottomnavigtionview_Fragment.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/nav">

    </com.google.android.material.bottomnavigation.BottomNavigationView>

</androidx.constraintlayout.widget.ConstraintLayout>

最後執行結果


上一篇
Android Studio菜鳥筆記 - Day24 - Fragment之add()、show()、hide()使用方法
下一篇
Android Studio菜鳥筆記 - Day26 - ViewPager2+Fragment
系列文
Android Studio - 30天菜鳥筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言