본문 바로가기

Android/기억창고

NavigationDrawer Activity 메뉴 만지작거리기(#1)

우선 Android Studio를 열고 NavigationDrawer Activity를 기본으로 프로젝트를 만들고 앱을 구동하면 처음에는 만족스럽습니다. 와우~~ 클릭 한 번으로 이런 기능이 만들어지네....

아래 그림을 보시기 바랍니다. 이렇게 트랜디한 앱을 내가 클릭 몇번으로 만들다니.... 안드로이드 쉽구만.... ㅉㅉ

그러나 그 것은 곧 실망감으로 바뀌게 됩니다. 뭔가 해보려고 해도 잘 안됩니다.

  1. 우선 메뉴를 내 마음대로 바꾸고 싶은데 잘 안됩니다.
  2. 그 다음에는 Fragment에서 새로운 Fragment를 호출하고 싶은데, 잘 안됩니다. getSupportfragmentmanager ().getfragments ( )를 사용해서 뭔가를 하라고 자꾸 나오는데 저는 실패했습니다. 결국 다른 방법을 써야 했습니다. Coding은 별로 필요업습니다.
  3. 그리고, 모든 화면에 나오는 fab(floating action button)이라고 기본적으로는 "편지"처럼 생긴 것이 모든 화면에 나옵니다. 이 걸 각 화면에서 다르게 작동하게 하고 싶은데 잘 안됩니다. 꼼수를 좀 썼습니다.
  4. 3번과 일부 겹치는 부분이 있는데 Option Menu를 각 화면별로 다르게 적용하려고 하니 딱 한 줄 만 더 추가해야 하는 경우가 있었습니다. ㅠㅠ

그래서 이 글을 적을 생각을 하게 되었습니다.

삽질을 최대한 방지하기 위해....... ㅋㅋㅋ

 

 

 

 

 

NavigationDrawer Activity에서 Drawer Menu 수정을 위한 기본사항

 관련된 파일은 다음과 같습니다. 눈치가 빠른 분은 어떻게 고치면 되는지 아실겁니다. ^^ 각 파일의 위치는 우측 그림을 참고해 주시기 바랍니다.

 

 

 

 

 

 

 

 

  • MainActivity.java : AppBarConfiguration에 menu 항목 전달
public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        ...
        mAppBarConfiguration = new AppBarConfiguration.Builder (
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send )
                .setDrawerLayout ( drawer )
                .build ();
        ...
    }
    .....
}

 

  • activity_main_drawer.xml: menu명, 메뉴 icon 정의(아래 구성에 대한 상세 내용은 안드로이드 개발자 싸이트의 메뉴 부분을 보시면 궁금증이 풀리실 겁니다.)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
        <item
            android:id="@+id/nav_tools"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/menu_tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="@string/menu_share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="@string/menu_send" />
        </menu>
    </item>

</menu>

 

  • mobile_navigation.xml: navigation 시작점(app:StartDestination), 메뉴ID, 메뉴에 해당하는 fragment와 layout과 menu의 label(fragment가 열렸을 때 나타나는 header title, 위의 경우와 다릅니다. ^^)
<?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/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.philm2k.navdrawerexample.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.philm2k.navdrawerexample.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.philm2k.navdrawerexample.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />

    <fragment
        android:id="@+id/nav_tools"
        android:name="com.philm2k.navdrawerexample.ui.tools.ToolsFragment"
        android:label="@string/menu_tools"
        tools:layout="@layout/fragment_tools" />

    <fragment
        android:id="@+id/nav_share"
        android:name="com.philm2k.navdrawerexample.ui.share.ShareFragment"
        android:label="@string/menu_share"
        tools:layout="@layout/fragment_share" />

    <fragment
        android:id="@+id/nav_send"
        android:name="com.philm2k.navdrawerexample.ui.send.SendFragment"
        android:label="@string/menu_send"
        tools:layout="@layout/fragment_send" />
</navigation>