먼저 레이아웃을 DrawerLayout으로 변경한다.


DrawerLayout 안에 메인 화면(FrameLayout)과 메뉴(ListView)를 추가한다.


activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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"
    tools:context=".MainActivity">
 
    <!-- 메인콘텐츠용 레이아웃 -->
 
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="20sp" />
    </FrameLayout>
 
    <!-- 슬라이드 메뉴용 레이아웃 -->
 
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/background_light" />
 
</android.support.v4.widget.DrawerLayout>
cs


레이아웃을 호출하기 위한 클래스 변수를 선언하고 각각 레이아웃을 호출한다.


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
 
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private TextView mTextView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mTextView = (TextView) findViewById(R.id.text);
 
        setupNavigationDrawer();
 
        // 목록 데이터를 설정
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1);
        adapter.add("Menu1");
        adapter.add("Menu2");
        adapter.add("Menu3");
        mDrawerList.setAdapter(adapter);
 
        if (savedInstanceState == null) {
            selectItem(0);
        }
    }
cs


Drawer를 열고 닫을 때의 이벤트를 설정한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    private void setupNavigationDrawer() {
        // NavigationDrawer의 그림자로 설정할 Drawable을 지정
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
        mDrawerList.setOnItemClickListener(this);
 
        // 드로워를 개폐 시에 이벤트를 받을 수 있게 함
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open,
                R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View view) {
            }
 
            @Override
            public void onDrawerOpened(View drawerView) {
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // DrawerToggle측의 옵션 메뉴 선택을 처리할 수 있도록
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // DrawerToggle측의 옵션 메뉴를 제어할 수 있도록
        mDrawerToggle.syncState();
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // DrawerToggle측에서 위/아래 변경을 제어할 수 있도록
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
 
    @Override
    public void onItemClick(AdapterView<?> adapterView, View parent,
                            int position, long id) {
        selectItem(position);
    }
 
    private void selectItem(int position) {
        ListAdapter adapter = mDrawerList.getAdapter();
        String item = (String) adapter.getItem(position);
        mTextView.setText("선택한 항목: " + item);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
cs


'Android' 카테고리의 다른 글

[Android] YouTube Api call Url  (0) 2019.03.22
YouTube Android Player API - Download  (0) 2019.03.20
[Android] AutoCompleteTextView 사용하기  (0) 2019.03.14
[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12

+ Recent posts