๐Ÿ“˜ Programming/Android

#10 ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ฉ”๋‰ด(Navigation Menu) Custom(DrawerLayout ์ปค์Šคํ…€ ์˜ˆ์ œ)

ํ•œ์ฝ”๋”ฉ 2020. 5. 5. 16:20
728x90
728x90
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
    android:id="@+id/drawer_layout"
    tools:context=".MainActivity">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <Button
           android:id="@+id/btn_open"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="์—ด๋ ค๋ผ!"
           />

    </LinearLayout>

    <!-- ์•กํ‹ฐ๋น„ํ‹ฐ ๋“œ๋Ÿฌ์šฐ ๋ ˆ์ด์•„์›ƒ์„ ๋ฉ”์ธ์— ํฌํ•จ์‹œํ‚จ๋‹ค -->
    <include layout="@layout/activity_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorAccent"
    android:orientation="vertical"
    android:id="@+id/drawer">

    <Button
        android:id="@+id/btn_close"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="๋ฉ”๋‰ด ๋‹ซ๊ธฐ"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="๋‚˜์˜ ๋ฉ”๋‰ด"
        android:textSize="30dp"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp"
        android:background="#D84646">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ํ…Œ์ŠคํŠธ ๋ฉ”๋‰ด1"
            android:gravity="center"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ํ…Œ์ŠคํŠธ ๋ฉ”๋‰ด2"
            android:gravity="center"/>

    </LinearLayout>



</LinearLayout>
package com.example.customnaviexample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.drawerlayout.widget.DrawerLayout.DrawerListener;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private View drawerView;

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

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerView = (View) findViewById(R.id.drawer);

        // ์—ด๋ ค๋ผ! ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋“œ๋กœ์šฐ ๋ฉ”๋‰ด๊ฐ€ ์—ด๋ฆผ
        Button btn_open = findViewById(R.id.btn_open);
        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(drawerView);
            }
        });

        // ์ด ๋ฌธ์žฅ ์ž…๋ ฅ ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•จํ•จ
       drawerLayout.setDrawerListener(listener);

       //
       drawerView.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View view, MotionEvent motionEvent) {
               return true;
           }
       });

       // ๋“œ๋กœ์šฐ ๋ฉ”๋‰ด์—์„œ ์ด ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์•ก์…˜ ( ๋‹ซ๋Š” ์•ก์…˜ )
       Button btn_close = (Button)findViewById(R.id.btn_close);
       btn_close.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               drawerLayout.closeDrawers();
           }
       });

    }

    // ํŠน์ • ์•ก์…˜์— ๋Œ€ํ•ด ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ
    DrawerLayout.DrawerListener listener = new DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(@NonNull View drawerView) {

        }

        @Override
        public void onDrawerClosed(@NonNull View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    };

}

728x90
๋ฐ˜์‘ํ˜•