๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Smart_Fan

[Smart_Fan] 06 - Android Studio : Navigation Drawer Menu ๊ตฌํ˜„ 02

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 7. 31.

 

 

 

 

[Smart_Fan] 05 - Android Studio : Navigation Drawer Menu ๊ตฌํ˜„ 01

[Smart_Fan] 04 - Arduino : ๋ฒ„ํŠผ์œผ๋กœ DC๋ชจํ„ฐ ์ „์› ๊ป๋‹ค ๋„๊ธฐ [Smart_Fan] 03 - Arduino : DC๋ชจํ„ฐ ์ž‘๋™ ์‹œํ‚ค๊ธฐ [Smart_Fan] 02 - Oven์œผ๋กœ ์•ฑ UI ๊ตฌ์ƒ ๋ฐ ์•„๋‘์ด๋…ธ ๊ตฌ์ž… [Smart_Fan] 01 - ์ฃผ์ œ ์„ ์ • ์ฃผ์ œ ์„ ์ • [20..

onelight-stay.tistory.com

 

Navigation Drawer Menu ๊ตฌํ˜„

[2020-07-12]

 

์—ฌํƒœ๊นŒ์ง€๋Š” ๋ฉ”๋‰ด๊ฐ€ ์˜†์—์„œ ๋‚˜์˜ค๊ณ  ๋‹ซํžˆ๋Š” ๊ฒƒ๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ 

๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํด๋ฆญ๋งŒ ๋  ๋ฟ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜์‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

๊ทธ๋ž˜์„œ fragment๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ ํ•ญ๋ชฉ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด์•˜๋‹ค.

 

 

#02. fragment๋ฅผ ์ด์šฉํ•ด ํ•ญ๋ชฉ ํด๋ฆญ์‹œ ๊ฐ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ์„ค์ •

 

๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— fragment์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ๋ช‡ ๊ฐ€์ง€ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ

๋ถ„ํ• ๋œ ํ™”๋ฉด๋“ค์„ ๋…๋ฆฝ์ ์œผ๋กœ ๊ตฌ์„ฑํ•˜๊ณ , 

ํ™”๋ฉด๋“ค์˜ ๊ฐ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ

์•กํ‹ฐ๋น„ํ‹ฐ์— ๋™์ ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋™์  UI ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

  • layout/fragment_home.xml, fragment_remote.xml, fragment_addremote.xml, fragment_explain.xml, fragment-settings.xml (๊ฐ ํŽ˜์ด์ง€ ํ™”๋ฉด)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/home" // ๊ฐ ํ•ญ๋ชฉ๋งˆ๋‹ค ํ…์ŠคํŠธ๋Š” ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ๋‹ค.
        android:layout_gravity="center"
        android:textSize="20sp"/>

</FrameLayout>

 

๊ฐ ํ•ญ๋ชฉ์˜ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด FrameLayout ํ˜•์‹์œผ๋กœ ์ž…๋ ฅํ•ด ์ฃผ์—ˆ๋‹ค.

 

 

  • navigation/main.xml
<?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/main"
    app:startDestination="@id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.example.smartfan.HomeFragment"
        android:label="Home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/nav_remote"
        android:name="com.example.smartfan.RemoteFragment"
        android:label="Remote"
        tools:layout="@layout/fragment_remote" />
    <fragment
        android:id="@+id/nav_addremote"
        android:name="com.example.smartfan.AddremoteFragment"
        android:label="Addremote"
        tools:layout="@layout/fragment_addremote" />
    <fragment
        android:id="@+id/nav_explain"
        android:name="com.example.smartfan.ExplainFragment"
        android:label="Explain"
        tools:layout="@layout/fragment_explain" />
    <fragment
        android:id="@+id/nav_settings"
        android:name="com.example.smartfan.SettingsFragment"
        android:label="Settings"
        tools:layout="@layout/fragment_settings" />
</navigation>

 

ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด ๊ฐ fragment์— id์™€ ๋ผ๋ฒจ์„ ์ž…๋ ฅํ•ด ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€์œผ๋ฉฐ

๋„๊ตฌ์— ๊ฐ ํ•ญ๋ชฉ์— ๋งž๋Š” ๋ ˆ์ด์•„์›ƒ์„ ์—ฐ๊ฒฐํ•ด ์ฃผ์—ˆ๋‹ค.

 

 

  • layout/activity_main.xml
        <LinearLayout
            android:id="@+id/layoutToolBar"   //id ์ถ”๊ฐ€
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary"

	……

        </LinearLayout>

        <fragment   // fragment ๋‚ด์šฉ ์ถ”๊ฐ€
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/layoutToolBar"
            app:layout_constraintBottom_toBottomOf="parent"
            android:id="@+id/navHostFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main"
            />

 

๊ธฐ์กด์— ์ž…๋ ฅํ•œ ๋‚ด์šฉ์—์„œ LinearLayout์˜ id์™€

fragment ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด ์•ž์ „์— ์ž…๋ ฅํ•œ ์ž…๋ ฅํ•œ ๊ฐ ํŽ˜์ด์ง€๋“ค์ด

ํ•ญ๋ชฉ๋“ค์„ ํด๋ฆญํ• ๋•Œ๋งˆ๋‹ค ๋น„์น  ์ˆ˜ ์žˆ๊ฒŒ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•ด ์ค€๋‹ค.

 

 

  • SettingsFragment.java, RemoteFragment.java, HomeFragment.java, ExplainFragment.java, AddremoteFragment.java (๊ฐ ํŽ˜์ด์ง€ ํ™”๋ฉด)
package com.example.smartfan;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

 

๊ฐ ํŽ˜์ด์ง€ ํ™”๋ฉด์— fragment๊ฐ€ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.

 

 

  • MainActivity.java
package com.example.smartfan;

import androidx.annotation.NonNull; 
import androidx.annotation.Nullable; 
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController; 
import androidx.navigation.NavDestination; 
import androidx.navigation.Navigation; 
import androidx.navigation.ui.NavigationUI; 

import android.os.Bundle;
import android.view.View;
import android.widget.TextView; 

import com.google.android.material.navigation.NavigationView;

……

        NavigationView navigationView = findViewById(R.id.navigationView);
        navigationView.setItemIconTintList(null);

        NavController navController = Navigation.findNavController(this, R.id.navHostFragment); 
        NavigationUI.setupWithNavController(navigationView, navController);

        final TextView textTitle = findViewById(R.id.textTitle);

        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
                textTitle.setText(destination.getLabel());
            }
        });  

    }
}

 

๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํ™”๋ฉด์„ ๋ณ€๊ฒฝํ•ด ์ค„ ์ˆ˜ ์žˆ๋„๋ก Navigation Controller์™€

ํ™”๋ฉด์„ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ํ™”๋ฉด์— ๋งž๋Š” ํƒ€์ดํ‹€๋กœ ํˆด๋ฐ”์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ด์ค„ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

 

 

์• ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ํ™•์ธํ•ด ๋ณด๋ฉด

๋ฉ”๋‰ด์— ์žˆ๋Š” ๊ฐ ํ•ญ๋ชฉ๋“ค์„ ํด๋ฆญํ•˜๋ฉด ๊ฐ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 


 

 

๊ฐ ํ•ญ๋ชฉ๋“ค์„ ๋ˆŒ๋Ÿฌ ํ•ด๋‹นํ•ญ๋ชฉ์˜ ํŽ˜์ด์ง€๊นŒ์ง€ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์„ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

 

์ด์ œ ์ด ๊ฐ ํŽ˜์ด์ง€๋“ค์„ ๋””์ž์ธํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€