Fragment+BottomNavigationView

Jยท2022๋…„ 9์›” 5์ผ
0

Android_Study

๋ชฉ๋ก ๋ณด๊ธฐ
4/5

๐Ÿ“Œ Fragment+BottomNavigationView

  • ํ™”๋ฉด ์ „ํ™˜
  • ํ•˜๋‹จ ๋ฉ”๋‰ด ๊ตฌ์„ฑ
  • ํ•˜๋‹จ ๋ฉ”๋‰ด ์„ ํƒ์œผ๋กœ ํ™”๋ฉด(Fragment) ์ „ํ™˜ ๊ธฐ๋Šฅ ๊ตฌํ˜„
  • ex) ์นด์นด์˜คํ†ก ๋ฉ”์ธ ํ™”๋ฉด ๋“ฑ...



๐Ÿ“ BottomNavigationView ๊ตฌํ˜„ + Fragment ์ž‘์„ฑ & ์—ฐ๊ฒฐ

1. menu ์ž‘์„ฑ (๋ ˆ์ด์•„์›ƒ)

bottom_navigation.xml

  • ํ•˜๋‹จ ๋ฉ”๋‰ด๋กœ ์‚ฌ์šฉํ•  Item ์ข…๋ฅ˜ ์ž‘์„ฑ
  • ๋ฉ”๋‰ด ์ด๋ฏธ์ง€ ๋“ฑ๋ก์€ drawble [ํด๋”]์— ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋„ฃ๊ธฐ
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu1"
        android:enabled="true"
        android:icon="@drawable/ic_info"
        android:title="fragment1"
        />

    <item
        android:id="@+id/menu2"
        android:enabled="true"
        android:icon="@drawable/ic_home"
        android:title="fragment2"
        />

    <item
        android:id="@+id/menu3"
        android:enabled="true"
        android:icon="@drawable/ic_settings"
        android:title="fragment3"
        />

</menu>



2. ๋ฉ”์ธ ํ™”๋ฉด์— BottomNavigaionView ์‚ฝ์ž…ํ•˜๊ธฐ (๋ ˆ์ด์•„์›ƒ)

activity_main.xml

  • LinearLayout์œผ๋กœ ์ค‘์•™์— ์ „ํ™˜ ๋œ Fragment๊ฐ€ ๋‚˜ํƒ€๋‚  ๊ณต๊ฐ„์„ ์ž‘์„ฑ
  • ํ•˜๋‹จ์— ์ด๋™ ๋ฉ”๋‰ดํƒญ์ธ BottomNavigationView ์ž‘์„ฑ
<?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">

    <LinearLayout
        android:id="@+id/layout00"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
        android:orientation="horizontal" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layout00"
        app:menu="@menu/bottom_navigation"
        />


    </androidx.constraintlayout.widget.ConstraintLayout>



3. ์ „ํ™˜ ํ•  Fragment ํ™”๋ฉด ๊ตฌํ˜„ (๋ ˆ์ด์•„์›ƒ)

fragment1.xml

  • ์ „ํ™˜ ์‹œ ๋ณด์—ฌ์งˆ Fragment ํ™”๋ฉด ๊ตฌํ˜„
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/blue">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="Fragment1"
        android:textColor="@color/black"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/mint">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/txt2"
        android:text="Fragment2"
        android:textColor="@color/black"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment3.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/yellow2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/txt3"
        android:text="Fragment3"
        android:textColor="@color/black"
        />

</androidx.constraintlayout.widget.ConstraintLayout>



4. Fragment ํ™”๋ฉด ๋ ˆ์ด์•„์›ƒ์˜ ์ž๋ฐ”ํŒŒ์ผ ์ž‘์„ฑ (Java)

Fragment1.java

  • ์ž‘์„ฑํ•œ Fragment(๋ ˆ์ด์•„์›ƒ)์™€ ์ž๋ฐ”ํŒŒ์ผ ์—ฐ๊ฒฐ
package com.example.fragment_bottomnavigationview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}

Fragment2.java

package com.example.fragment_bottomnavigationview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, container, false);
    }
}

Fragment3.java

package com.example.fragment_bottomnavigationview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment3 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment3, container, false);
    }
}

5. Fragment์™€ Menu ์—ฐ๊ฒฐ

MainActivity.java

  • Fragment๋ฅผ BottomNavigationView์˜ ๊ฐ ๋ฉ”๋‰ด๋“ค๊ณผ ์—ฐ๊ฒฐ
  • ๊ฐ ๋ฉ”๋‰ด ์„ ํƒ ์‹œ Fragment ์ „ํ™˜ ๊ธฐ๋Šฅ ๊ตฌํ˜„
package com.example.fragment_bottomnavigationview;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager = getSupportFragmentManager();
    private Fragment1 fragment1 = new Fragment1();
    private Fragment2 fragment2 = new Fragment2();
    private Fragment3 fragment3 = new Fragment3();

    private BottomNavigationView bottomNavigationView;
    private LinearLayout containerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        containerView = findViewById(R.id.layout00);

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.layout00, fragment1, null);
        transaction.commit();

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu1:
                        Log.wtf("MainActivity", "menu1 Click");       //Log ๋ณด๊ธฐ
                        fragmentManager.beginTransaction().replace(R.id.layout00, fragment1).commit();
                        transaction.addToBackStack(null);   //.addToBackStack(null) ์ถ”๊ฐ€ ์‹œ -> ์Šคํƒ์— ์Œ“์ด๋ฏ€๋กœ ๋’ค๋กœ๊ฐ€๊ธฐ ์‹œ ์Šคํƒ์— ์žˆ๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ onDestory ๋œ๋‹ค.
                        break;                              // ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ธฐ๋Šฅ.

                    case R.id.menu2:
                        Log.wtf("MainActivity", "menu2 Click");
                        fragmentManager.beginTransaction().replace(R.id.layout00, fragment2).commit();
                        transaction.addToBackStack(null);
                        break;

                    case R.id.menu3:
                        Log.wtf("MainActivity", "menu3 Click");
                        fragmentManager.beginTransaction().replace(R.id.layout00, fragment3).commit();
                        transaction.addToBackStack(null);
                        break;
                }
                return true;
            }
        });
    }
}



๐Ÿ“Œ ์™„์„ฑ






์ž‘์„ฑํ•œ ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ github : Android_Study/fragment_bottomnavigationview/

profile
Hello World!

0๊ฐœ์˜ ๋Œ“๊ธ€