한 걸음 두 걸음

android drawerLayout / NavigaionDrawer활용하기 본문

FrontEnd/Android

android drawerLayout / NavigaionDrawer활용하기

언제나 변함없이 2019. 3. 22. 21:35
반응형

참고

https://recipes4dev.tistory.com/139
https://recipes4dev.tistory.com/140


DrawerLayout

Code

activity_main.xml

android.support.v4.widget.DrawerLayout 내에 TextView 두 개 준게 다임.
기본적으로, 주 화면으로 표시될 뷰를 첫 번째 자식(Child)으로 선언하고, Drawer로 사용될 뷰를 마지막 자식(Child)으로 선언합니다.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/activity_main_open_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:text="OPEN"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/activity_main_close_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="16dp"
        android:text="CLOSE"
        app:layout_constraintStart_toEndOf="@+id/activity_main_open_button"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/activity_main_lock_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:text="Lock"
        app:layout_constraintStart_toEndOf="@+id/activity_main_close_button"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/activity_main_drawer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/activity_main_open_button">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00BCD4"
            android:gravity="center"
            android:text="CONTENTS"
            android:textSize="48sp" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#009688"
            android:gravity="center"
            android:text="DRAWER"
            android:textSize="48sp" />
    </android.support.v4.widget.DrawerLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.java

이벤트 처리하고 끝, drawerLayout에 drawer와 contents를 설정
(오른쪽에서 왼쪽으로 끌었을 때 drawer가 나오는 것이나, drawer가 나왔을 때 기존에 있던 뷰가 어두워지는 것은 drawerLayout에서 자동으로 제공하는 기능이다. 따로 구현해주지 않아도 된다.)

package com.gkskfhdlstmapk.hanpinetree.bcsd_navi_listview_calc;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mOpenButton;
    private Button mCloseButton;
    private CheckBox mLockCheckbox;
    private DrawerLayout drawerLayout;


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

        init();

    }
    public void init(){
        mOpenButton = (Button)findViewById(R.id.activity_main_open_button);
        mCloseButton = (Button)findViewById(R.id.activity_main_close_button);
        mLockCheckbox = (CheckBox)findViewById(R.id.activity_main_lock_checkbox);
        drawerLayout = (DrawerLayout)findViewById(R.id.activity_main_drawer);

        mOpenButton.setOnClickListener(this);
        mCloseButton.setOnClickListener(this);
        mLockCheckbox.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_main_open_button:
                if(!drawerLayout.isDrawerOpen(Gravity.LEFT)){drawerLayout.openDrawer(Gravity.LEFT);}
                break;
            case R.id.activity_main_close_button:
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){drawerLayout.closeDrawer(Gravity.LEFT);}
                break;
            case R.id.activity_main_lock_checkbox:
                if(mLockCheckbox.isChecked()){
                    if(drawerLayout.isDrawerOpen(Gravity.LEFT))
                        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
                    else drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                }else
                    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                break;
        }
    }
}
반응형