코드네임 :
📱 모바일프로그래밍 - Navigation 📱 본문
<밑에 네비게이션 추가하는 법 사진 찍어놓음>
# 과제 코드 (네비게이션 코드 말구)
#inside MainActivity.java, 
# protected void onCreate(Bundle savedInstanceState) { ~ }); 다음
//fragment 객체를 메모리에 생성하고 준비해두기
BookFragment bookFragment = new BookFragment();
CenterFragment centerFragment = new CenterFragment();
HomeFragment homeFragment = new HomeFragment();
// getSupportFragmentManager(): 액티비티에서 프래그먼트를 관리하는 FragmentManager 객체를 반환
// beginTransaction(): 프래그먼트를 추가, 교체, 삭제 등의 작업을 수행하기 위해 트랜잭션을 시작
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// R.id.rootlayout에 BookFragment를 추가
// add(containerViewId, fragment): 특정 레이아웃(containerViewId)에 지정된 프래그먼트를 추가
// R.id.rootlayout: 프래그먼트를 추가할 XML 레이아웃의 ID
fragmentTransaction.add(R.id.rootlayout, bookFragment);
//트랜잭션을 완료하고 적용 (이 호출 이후 프래그먼트가 화면에 표시됨)
fragmentTransaction.commit();
// XML 레이아웃에서 하단 네비게이션 바(BottomNavigationView)의 id를 찾아서 객체로 가져옴
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
// 하단 네비게이션 바에서 탭 선택 시 실행될 동작을 설정
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        //새로운 프래그먼트 트랜잭션을 시작 (탭 선택 시 이전 트랜잭션과는 별개의 트랜잭션을 시작)
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        // 사용자가 tabBook 탭을 선택했을 때 BookFragment로 화면을 전환
        if (item.getItemId() == R.id.tabBook){
            fragmentTransaction.replace(R.id.rootlayout, bookFragment).commit();
            return true;
        } else if (item.getItemId() == R.id.tabCenter){
            //replace(containerViewId, fragment): 기존의 프래그먼트를 제거하고 새로운 프래그먼트를 교체
            //commit(): 트랜잭션을 실행하여 교체를 완료
            fragmentTransaction.replace(R.id.rootlayout, centerFragment).commit();
            return true;
        } else if (item.getItemId() == R.id.tabHome){
            fragmentTransaction.replace(R.id.rootlayout, homeFragment).commit();
            return true;
        } else return false;
    }
Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rootlayout"
        android:layout_weight="1"/>
<!--    여백은 너 다 가져-->
<!--    네비게이션바 생성해봅쉬다-->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_nav"
        app:menu="@menu/bottom_menu"></com.google.android.material.bottomnavigation.BottomNavigationView>
</LinearLayout>





'👾Android > Android_JAVA' 카테고리의 다른 글
| 📱 모바일프로그래밍 - 어댑터뷰 : 리사이클러뷰 📱 (0) | 2024.11.21 | 
|---|---|
| 📱 모바일프로그래밍 - 프래그먼트 생성주기 메소드 📱 ( 이거 질문해야됨!!!) (1) | 2024.11.21 | 
| 📱 모프 과제 헛둘헛둘 📱 (1) | 2024.11.21 | 
| 📱 모바일프로그래밍 - Fragment 📱 (0) | 2024.11.12 | 
| 📱 모바일프로그래밍 - 액티비티 생명주기 📱 (0) | 2024.11.12 |