μΉ΄ν…Œκ³ λ¦¬ 보관물: Android

Android

λ‹€λ₯Έ ν™œλ™μ—μ„œ λ™μΌν•œ 탐색 μ°½

developer.android.com μ›Ή μ‚¬μ΄νŠΈ 의 μžμŠ΅μ„œμ— ν‘œμ‹œλœ κ²ƒμ²˜λŸΌ μž‘λ™ν•˜λŠ” 탐색 λ“œλ‘œμ–΄λ₯Ό μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€ . κ·ΈλŸ¬λ‚˜ 이제 λ‚΄ μ‘μš© ν”„λ‘œκ·Έλž¨μ˜ μ—¬λŸ¬ ν™œλ™μ„ μœ„ν•΄ NavigationDrawer.classμ—μ„œ λ§Œλ“  ν•˜λ‚˜μ˜ Navigation Drawerλ₯Ό μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

λ‚΄ μ§ˆλ¬Έμ€, μ—¬κΈ° λˆ„κ΅°κ°€κ°€ μž‘μ€ νŠœν† λ¦¬μ–Όμ„ λ§Œλ“€ 수 μžˆλ‹€λ©΄, μ—¬λŸ¬ ν™œλ™μ— ν•˜λ‚˜μ˜ 탐색 μ„œλžμ„ μ‚¬μš©ν•˜λŠ” 방법을 μ„€λͺ…ν•©λ‹ˆλ‹€.

μ—¬λŸ¬ ν™œλ™μ— λŒ€ν•œ 이 Answer Android Navigation Drawerμ—μ„œ 처음 μ½μ—ˆμŠ΅λ‹ˆλ‹€.

ν•˜μ§€λ§Œ λ‚΄ ν”„λ‘œμ νŠΈμ—μ„œ μž‘λ™ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

public class NavigationDrawer extends Activity {
public DrawerLayout drawerLayout;
public ListView drawerList;
private ActionBarDrawerToggle drawerToggle;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0) {

        public void onDrawerClosed(View view) {
            getActionBar().setTitle(R.string.app_name);
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(R.string.menu);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    layers = getResources().getStringArray(R.array.layers_array);
    drawerList = (ListView) findViewById(R.id.left_drawer);
    View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null);
    drawerList.addHeaderView(header, null, false);
    drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
            layers));
    View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
            R.layout.drawer_list_footer, null, false);
    drawerList.addFooterView(footerView);

    drawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            map.drawerClickEvent(pos);
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}
}

이 ν™œλ™μ—μ„œ λ‚˜λŠ” 탐색 μ„œλžμ„ κ°–κ³  μ‹Άμ–΄μ„œ β€˜NavigationDrawer’λ₯Ό ν™•μž₯ν•˜κ³  λ‹€λ₯Έ ν™œλ™μ—μ„œλŠ” λ™μΌν•œ 탐색 μ„œλžμ„ μ‚¬μš©ν•˜λ €κ³ ν•©λ‹ˆλ‹€.

  public class SampleActivity extends NavigationDrawer {...}

무엇을 λ°”κΏ”μ•Όν• μ§€ λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€ …



λ‹΅λ³€

탐색 λ“œλ‘œμ–΄λ₯Ό μ›ν•˜λ©΄ 쑰각을 μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€. μ§€λ‚œμ£Όμ—μ΄ νŠœν† λ¦¬μ–Όμ„ λ”°λžμœΌλ©° ν›Œλ₯­ν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€.

http://developer.android.com/training/implementing-navigation/nav-drawer.html

이 νŠœν† λ¦¬μ–Όμ—μ„œ μƒ˜ν”Œ μ½”λ“œλ₯Ό λ‹€μš΄λ‘œλ“œν•˜μ—¬μ΄λ₯Ό μˆ˜ν–‰ν•˜λŠ” 방법을 확인할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.


νŒŒνŽΈμ—†μ΄ :

이것은 BaseActivity μ½”λ“œμž…λ‹ˆλ‹€.

public class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;
    private Map map;

    protected void onCreate(Bundle savedInstanceState)
    {
        // R.id.drawer_layout should be in every activity with exactly the same id.
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0)
        {
            public void onDrawerClosed(View view)
            {
                getActionBar().setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView)
            {
                getActionBar().setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        layers = getResources().getStringArray(R.array.layers_array);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null);
        drawerList.addHeaderView(header, null, false);
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
                layers));
        View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                R.layout.drawer_list_footer, null, false);
        drawerList.addFooterView(footerView);

        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                map.drawerClickEvent(pos);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
}

탐색 λ“œλ‘œμ–΄κ°€ ν•„μš”ν•œ λ‹€λ₯Έ λͺ¨λ“  ν™œλ™μ€ ν™œλ™ 자체 λŒ€μ‹ μ΄ ν™œλ™μ„ ν™•μž₯ν•΄μ•Όν•©λ‹ˆλ‹€. 예 :

public class AnyActivity extends BaseActivity
{
    //Because this activity extends BaseActivity it automatically has the navigation drawer
    //You can just write your normal Activity code and you don't need to add anything for the navigation drawer
}

XML

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- Put what you want as your normal screen in here, you can also choose for a linear layout or any other layout, whatever you prefer -->
    </FrameLayout>
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

νŽΈμ§‘ν•˜λ‹€:

λ‚˜λŠ” μ•½κ°„μ˜ 어렀움을 κ²ͺμ—ˆμœΌλ―€λ‘œ NullPointerExceptionsλ₯Ό μ–»λŠ”λ‹€λ©΄ 여기에 해결책이 μžˆμŠ΅λ‹ˆλ‹€. BaseActivityμ—μ„œ onCreate ν•¨μˆ˜λ₯Όλ‘œ λ³€κ²½ν•˜μ‹­μ‹œμ˜€ protected void onCreateDrawer(). λ‚˜λ¨Έμ§€λŠ” λ™μΌν•˜κ²Œ μœ μ§€ 될 수 μžˆμŠ΅λ‹ˆλ‹€. BaseActivityλ₯Ό ν™•μž₯ν•˜λŠ” ν™œλ™μ—μ„œ μ½”λ“œλ₯Ό λ‹€μŒ μˆœμ„œλ‘œ λ„£μœΌμ‹­μ‹œμ˜€.

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    super.onCreateDrawer();

이것은 λ‚΄ 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 λ„μ›€μ΄λ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ„μ›€μ΄λ˜κΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.

질문이 μžˆμœΌμ‹œλ©΄ μ—¬λŸ¬ ν™œλ™μ΄ 포함 된 탐색 창을 λ§Œλ“œλŠ” λ°©λ²•μž…λ‹ˆλ‹€.


νŽΈμ§‘ 2 :

@GregDan이 λ§ν–ˆλ“―μ΄ onCreateDrawerλ₯Ό BaseActivityμž¬μ •μ˜ setContentView()ν•˜κ³  호좜 ν•  수 μžˆμŠ΅λ‹ˆλ‹€ .

@Override
public void setContentView(@LayoutRes int layoutResID)
{
    super.setContentView(layoutResID);
    onCreateDrawer() ;
}

λ‹΅λ³€

졜고의 κ΅¬ν˜„μ„ μ°Ύμ•˜μŠ΅λ‹ˆλ‹€. κ·Έκ²ƒμ€μ—μ„œμ˜ ꡬ글 I / O 2014 μ‘μš© ν”„λ‘œκ·Έλž¨.

그듀은 Kevinκ³Ό 같은 접근법을 μ‚¬μš©ν•©λ‹ˆλ‹€. I / O μ•±μ˜ λΆˆν•„μš”ν•œ λͺ¨λ“  ν•­λͺ©μ—μ„œ μžμ‹ μ„ 좔상화 ν•  수 μžˆλ‹€λ©΄ ν•„μš”ν•œ λͺ¨λ“  것을 μΆ”μΆœ ν•  수 있으며 Google은 탐색 λ“œλ‘œμ–΄ νŒ¨ν„΄μ˜ μ˜¬λ°”λ₯Έ μ‚¬μš©λ²•μ„ 보μž₯ν•©λ‹ˆλ‹€. 각 ν™œλ™μ—λŠ” 선택적 DrawerLayout으둜 κΈ°λ³Έ λ ˆμ΄μ•„μ›ƒμ΄ μžˆμŠ΅λ‹ˆλ‹€. ν₯미둜운 뢀뢄은 λ‹€λ₯Έ ν™”λ©΄μœΌλ‘œ μ΄λ™ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€. λ‹€μŒ BaseActivityκ³Ό 같이 κ΅¬ν˜„λ©λ‹ˆλ‹€ .

private void goToNavDrawerItem(int item) {
        Intent intent;
        switch (item) {
            case NAVDRAWER_ITEM_MY_SCHEDULE:
                intent = new Intent(this, MyScheduleActivity.class);
                startActivity(intent);
                finish();
                break;

이것은 ν˜„μž¬ 쑰각을 쑰각 νŠΈλžœμž­μ…˜μœΌλ‘œ λŒ€μ²΄ν•˜λŠ” 일반적인 방법과 λ‹€λ¦…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ‚¬μš©μžλŠ” μ‹œκ°μ  차이λ₯Ό λ°œκ²¬ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€.


λ‹΅λ³€

λ”°λΌμ„œμ΄ 닡변은 λͺ‡ λ…„ λŠ¦μ—ˆμ§€λ§Œ λˆ„κ΅°κ°€λŠ” 그것을 감사 ν•  κ²ƒμž…λ‹ˆλ‹€. AndroidλŠ” μ—¬λŸ¬ ν™œλ™μ΄ 포함 된 ν•˜λ‚˜μ˜ 탐색 창을보닀 μ‰½κ²Œ β€‹β€‹μ‚¬μš©ν•  μˆ˜μžˆλŠ” μƒˆλ‘œμš΄ μœ„μ ―μ„ μ œκ³΅ν–ˆμŠ΅λ‹ˆλ‹€.

android.support.design.widget.NavigationViewλŠ” λͺ¨λ“ˆ 식이며 메뉴 폴더에 자체 λ ˆμ΄μ•„μ›ƒμ΄ μžˆμŠ΅λ‹ˆλ‹€. μ‚¬μš©ν•˜λŠ” 방법은 λ‹€μŒκ³Ό 같은 λ°©μ‹μœΌλ‘œ xml λ ˆμ΄μ•„μ›ƒμ„ λž˜ν•‘ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

  1. 루트 λ ˆμ΄μ•„μ›ƒμ€ <include ... />λž©ν•‘λ˜λŠ” λ ˆμ΄μ•„μ›ƒ (2 μ°Έμ‘°)κ³Ό android.support.design.widget.NavigationView의 두 μžμ‹μ„ 포함 ν•˜λŠ” android.support.v4.widget.DrawerLayoutμž…λ‹ˆλ‹€.

    <android.support.v4.widget.DrawerLayout
        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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

nav_header_main은 Navigation Drawar의 헀더에 λ°©ν–₯ = 수직 인 LinearLayoutμž…λ‹ˆλ‹€.

activity_main_drawerλŠ” res / menu λ””λ ‰ν† λ¦¬μ˜ 메뉴 xmlμž…λ‹ˆλ‹€. μ„ νƒν•œ ν•­λͺ©κ³Ό 그룹을 포함 ν•  수 μžˆμŠ΅λ‹ˆλ‹€. AndroidStudio Galleryλ₯Ό μ‚¬μš©ν•˜λŠ” 경우 λ§ˆλ²•μ‚¬κ°€ κΈ°λ³Έ λ§ˆλ²•μ‚¬λ₯Ό λ§Œλ“€κ³  μ˜΅μ…˜μ΄ 무엇인지 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

  1. μ•± λ°” λ ˆμ΄μ•„μ›ƒμ€ 일반적으둜 android.support.design.widget.CoordinatorLayout이며 μ—¬κΈ°μ—λŠ” android.support.design.widget.AppBarLayout (android.support.v7.widget.Toolbar 포함) 및 <include ... >for. μ‹€μ œ λ‚΄μš© (3 μ°Έμ‘°)

    <android.support.design.widget.CoordinatorLayout
        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="yourpackage.MainActivity">
    
     <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.design.widget.AppBarLayout>
    
    <include layout="@layout/content_main" />
  2. μ½˜ν…μΈ  λ ˆμ΄μ•„μ›ƒμ€ μ›ν•˜λŠ” λ ˆμ΄μ•„μ›ƒμ΄ 될 수 μžˆμŠ΅λ‹ˆλ‹€. 이것은 ν™œλ™μ˜ μ£Όμš” λ‚΄μš©μ„ ν¬ν•¨ν•˜λŠ” λ ˆμ΄μ•„μ›ƒμž…λ‹ˆλ‹€ (탐색 μ„œλžμ΄λ‚˜ μ•± λ°”λŠ” ν¬ν•¨λ˜μ§€ μ•ŠμŒ).

이제이 λͺ¨λ“  κ²ƒμ˜ λ©‹μ§„ 점은이 두 λ ˆμ΄μ•„μ›ƒμœΌλ‘œ 각 ν™œλ™μ„ λž˜ν•‘ ν•  수 μžˆμ§€λ§Œ NavigationView (1 단계 μ°Έμ‘°)κ°€ 항상 activity_main_drawer (λ˜λŠ” 무엇이든)λ₯Ό 가리 ν‚€λ„λ‘ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. 즉, λͺ¨λ“  ν™œλ™μ— λŒ€ν•΄ λ™μΌν•œ (*) 탐색 창을 κ°–κ²Œλ©λ‹ˆλ‹€.

  • 그것듀은 NavigationView 의 λ™μΌν•œ μΈμŠ€ν„΄μŠ€ λŠ” μ•„λ‹ˆμ§€λ§Œ κ³΅μ •ν•˜κ²Œ λ§ν•˜λ©΄ μœ„μ—μ„œ μ„€λͺ…ν•œ BaseActivity μ†”λ£¨μ…˜μœΌλ‘œλŠ” λΆˆκ°€λŠ₯ν–ˆμŠ΅λ‹ˆλ‹€.

λ‹΅λ³€

ν™œλ™ 그룹간에 곡톡 탐색 λ“œλ‘œμ–΄λ₯Ό μž¬μ‚¬μš©ν•˜λŠ” κ°€μž₯ μ‰¬μš΄ 방법

app_base_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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">

    <FrameLayout
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_test"
        />
</android.support.v4.widget.DrawerLayout>

AppBaseActivity.java

/*
* This is a simple and easy approach to reuse the same
* navigation drawer on your other activities. Just create
* a base layout that conains a DrawerLayout, the
* navigation drawer and a FrameLayout to hold your
* content view. All you have to do is to extend your
* activities from this class to set that navigation
* drawer. Happy hacking :)
* P.S: You don't need to declare this Activity in the
* AndroidManifest.xml. This is just a base class.
*/
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public abstract class AppBaseActivity extends AppCompatActivity implements MenuItem.OnMenuItemClickListener {
    private FrameLayout view_stub; //This is the framelayout to keep your content view
    private NavigationView navigation_view; // The new navigation view from Android Design Library. Can inflate menu resources. Easy
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private Menu drawerMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.app_base_layout);// The base layout that contains your navigation drawer.
        view_stub = (FrameLayout) findViewById(R.id.view_stub);
        navigation_view = (NavigationView) findViewById(R.id.navigation_view);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        drawerMenu = navigation_view.getMenu();
        for(int i = 0; i < drawerMenu.size(); i++) {
          drawerMenu.getItem(i).setOnMenuItemClickListener(this);
        }
        // and so on...
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /* Override all setContentView methods to put the content view to the FrameLayout view_stub
     * so that, we can make other activity implementations looks like normal activity subclasses.
     */
    @Override
    public void setContentView(int layoutResID) {
        if (view_stub != null) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            View stubView = inflater.inflate(layoutResID, view_stub, false);
            view_stub.addView(stubView, lp);
        }
    }

    @Override
    public void setContentView(View view) {
        if (view_stub != null) {
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            view_stub.addView(view, lp);
        }
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (view_stub != null) {
            view_stub.addView(view, params);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                // handle it
                break;
            case R.id.item2:
                // do whatever
                break;
            // and so on...
        }
        return false;
    }
}

λ‹΅λ³€

μ›λž˜ ν¬μŠ€ν„°κ°€ μš”κ΅¬ν•˜λŠ” 것을 μˆ˜ν–‰ν•˜λ €λŠ” λ‹€λ₯Έ μ‚¬λžŒμ€ Kevin이 λ§ν•œ 방식 λŒ€μ‹  쑰각을 μ‚¬μš©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. 이λ₯Ό μˆ˜ν–‰ν•˜λŠ” 방법에 λŒ€ν•œ ν›Œλ₯­ν•œ μžμŠ΅μ„œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer

ν”„λž˜κ·Έλ¨ΌνŠΈ λŒ€μ‹  μ•‘ν‹°λΉ„ν‹°λ₯Ό μ‚¬μš©ν•˜λ„λ‘ μ„ νƒν•˜λ©΄ μƒˆ μ•‘ν‹°λΉ„ν‹°λ‘œ 이동할 λ•Œλ§ˆλ‹€ 탐색 λ“œλ‘œμ–΄κ°€ λ‹€μ‹œ μž‘μ„±λ˜λŠ” λ¬Έμ œκ°€ λ°œμƒν•©λ‹ˆλ‹€. 이둜 인해 맀번 탐색 μ„œλžμ΄λ³΄κΈ° ν‰ν•˜κ³  느리게 λ Œλ”λ§λ©λ‹ˆλ‹€.


λ‹΅λ³€

λ‚΄ μ œμ•ˆμ€ : ν™œλ™μ„ μ „ν˜€ μ‚¬μš©ν•˜μ§€ 말고 쑰각을 μ‚¬μš©ν•˜κ³  첫 번째 쑰각을 ν‘œμ‹œν•˜λŠ” μ»¨ν…Œμ΄λ„ˆ (예 : μ„ ν˜• λ ˆμ΄μ•„μ›ƒ)μ—μ„œ κ΅μ²΄ν•˜μ‹­μ‹œμ˜€.

μ½”λ“œλŠ” Android Developer Tutorialsμ—μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ‚¬μš©μž μ •μ˜ν•˜λ©΄λ©λ‹ˆλ‹€.

http://developer.android.com/training/implementing-navigation/nav-drawer.html

μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ 점점 더 λ§Žμ€ ν”„λž˜κ·Έλ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•΄μ•Όν•˜λ©° μ™ΈλΆ€ μ• ν”Œλ¦¬μΌ€μ΄μ…˜κ³Ό λ³„λ„λ‘œ AndroidManifest.xmlμ—μ„œ μ–ΈκΈ‰ ν•œ λ„€ κ°€μ§€ κΈ°λ³Έ ν™œλ™ 만 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ— κ΅­ν•œλ˜μ–΄μ•Όν•©λ‹ˆλ‹€ (FacebookActivity).

  1. SplashActivity : 쑰각을 μ‚¬μš©ν•˜μ§€ μ•Šκ³  FullScreen ν…Œλ§ˆλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

  2. LoginSignUpActivity : NavigationDrawerλ₯Ό μ „ν˜€ ν•„μš”λ‘œν•˜μ§€ μ•Šκ³  λ’€λ‘œ 단좔도 ν•„μš”ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 일반 도ꡬ λͺ¨μŒμ„ μ‚¬μš©ν•˜κΈ° λ§Œν•˜λ©΄ μ΅œμ†Œν•œ 3 개 λ˜λŠ” 4 개의 쑰각이 ν•„μš”ν•©λ‹ˆλ‹€. μ•‘μ…˜ λ°”μ—†λŠ” ν…Œλ§ˆ μ‚¬μš©

  3. HomeActivity λ˜λŠ” DashBoard Activity : μ•‘μ…˜ λ°” μ—†μŒ ν…Œλ§ˆλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. μ—¬κΈ°μ—λŠ” 탐색 λ“œλ‘œμ–΄κ°€ ν•„μš”ν•˜λ©°, κ·Έ λ‹€μŒμ— λ‚˜μ˜€λŠ” λͺ¨λ“  화면은 곡유 λ“œλ‘œμ–΄μ™€ ν•¨κ»˜ λ¦¬ν”„λ³΄κΈ°κΉŒμ§€ ν”„λž˜κ·Έλ¨ΌνŠΈ λ˜λŠ” 쀑첩 된 ν”„λž˜κ·Έλ¨ΌνŠΈμž…λ‹ˆλ‹€. 이 μ•‘ν‹°λΉ„ν‹°μ—μ„œλŠ” λͺ¨λ“  μ„€μ •, μ‚¬μš©μž ν”„λ‘œν•„ 등이 쑰각으둜 ν‘œμ‹œλ©λ‹ˆλ‹€. μ—¬κΈ°μ˜ 쑰각은 λ°± μŠ€νƒμ— μΆ”κ°€λ˜μ§€ μ•ŠμœΌλ©° μ„œλž 메뉴 ν•­λͺ©μ—μ„œ μ—΄λ¦½λ‹ˆλ‹€. μ„œλž λŒ€μ‹  λ’€λ‘œ λ²„νŠΌμ΄ ν•„μš”ν•œ 쑰각의 경우 μ•„λž˜μ— λ„€ 번째 μ’…λ₯˜μ˜ ν™œλ™μ΄ μžˆμŠ΅λ‹ˆλ‹€.

  4. μ„œλžμ΄μ—†λŠ” ν™œλ™. 이 μ•‘ν‹°λΉ„ν‹°μ—λŠ” 상단에 λ’€λ‘œ λ²„νŠΌμ΄ 있으며 λ‚΄λΆ€ 쑰각은 λ™μΌν•œ μž‘μ—… ν‘œμ‹œ 쀄을 κ³΅μœ ν•©λ‹ˆλ‹€. 탐색 내역이 μžˆμœΌλ―€λ‘œ μ΄λŸ¬ν•œ 쑰각은 λ°± μŠ€νƒμ— μΆ”κ°€λ©λ‹ˆλ‹€.

[μΆ”κ°€ 지침은 https://stackoverflow.com/a/51100507/787399 μ°Έμ‘° ]

ν–‰λ³΅ν•œ μ½”λ”©!


λ‹΅λ³€

κΈ°λ³Έ ν™œλ™μ—μ„œμ΄ μ½”λ“œλ₯Ό μ—…λ°μ΄νŠΈν•˜μ‹­μ‹œμ˜€. 그리고 ν™œλ™ XML에 drawer_list_headerλ₯Ό ν¬ν•¨μ‹œν‚€λŠ” 것을 μžŠμ§€ λ§ˆμ‹­μ‹œμ˜€.

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.drawer_list_header);

ν™œλ™μ— request ()λ₯Ό μ‚¬μš©ν•˜μ§€ λ§ˆμ‹­μ‹œμ˜€. κ·ΈλŸ¬λ‚˜ μ—¬μ „νžˆ 이미지λ₯Ό ν΄λ¦­ν•˜λ©΄ μ„œλžμ΄ 보이지 μ•ŠμŠ΅λ‹ˆλ‹€. λ“œλž˜κ·Έν•˜λ©΄ λͺ©λ‘ ν•­λͺ©μ΄ 없어도 μ„œλžμ΄ λ³΄μž…λ‹ˆλ‹€. λ‚˜λŠ” 많이 μ‹œλ„ν–ˆμ§€λ§Œ μ„±κ³΅ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό μœ„ν•΄ μš΄λ™μ΄ ν•„μš”ν•©λ‹ˆλ‹€ …


이 글은 Android μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜λ˜μ—ˆκ³  λ‹˜μ— μ˜ν•΄ 에 μž‘μ„±λμŠ΅λ‹ˆλ‹€.