๐Ÿ’ปTech/๐ŸŒตandroid

[์•ˆ๋“œ๋กœ์ด๋“œ] ๊ฐ„๋‹จํ•˜๊ฒŒ ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด ์ ์šฉ

_viper_ 2015. 9. 25. 11:12
๋ฐ˜์‘ํ˜•

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋„ค๋น„๊ฒŒ์ด์…˜ ๋“œ๋กœ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € libs ํด๋”์— android-support-v4.jar ํด๋”๋ฅผ ์ถ”๊ฐ€ ํ•ฉ๋‹ˆ๋‹ค.

 

activity_slide_menu.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dl_activity_main_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="40dp" />
    </FrameLayout>

    <ListView
        android:id="@+id/lv_activity_main_nav_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#EDEDED"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

 

SlideMenuActiviy.java

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;

/**
 * ๋„ค๋น„๊ฒŒ์ด์…˜ ๋“œ๋กœ์–ด ์ ์šฉ
 * 
 * @author
 * @version
 * @since
 * @created 2015. 9. 24.
 */
public class SlideMenuActivity extends Activity {

    private final String[] navItems = {"Brown", "Cadet Blue", "Dark Olive Green", "Dark Orange", "Golden Rod"};

    private ListView lvNavList;

    private FrameLayout flContainer;

    private DrawerLayout dlDrawer;

    private Button btn;

    @Override
    public void onBackPressed() {
        if (dlDrawer.isDrawerOpen(lvNavList)) {
            dlDrawer.closeDrawer(lvNavList);
        } else {
            super.onBackPressed();
        }
    }

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

        lvNavList = (ListView)findViewById(R.id.lv_activity_main_nav_list);
        flContainer = (FrameLayout)findViewById(R.id.fl_activity_main_container);
        btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "open", Toast.LENGTH_SHORT).show();
                dlDrawer.openDrawer(lvNavList);
            }
        });

        dlDrawer = (DrawerLayout)findViewById(R.id.dl_activity_main_drawer);

        lvNavList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, navItems));
        lvNavList.setOnItemClickListener(new DrawerItemClickListener());
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            switch (position) {
                case 0:
                    flContainer.setBackgroundColor(Color.parseColor("#A52A2A"));
                    break;
                case 1:
                    flContainer.setBackgroundColor(Color.parseColor("#5F9EA0"));
                    break;
                case 2:
                    flContainer.setBackgroundColor(Color.parseColor("#556B2F"));
                    break;
                case 3:
                    flContainer.setBackgroundColor(Color.parseColor("#FF8C00"));
                    break;
                case 4:
                    flContainer.setBackgroundColor(Color.parseColor("#DAA520"));
                    break;
            }
            dlDrawer.closeDrawer(lvNavList);

        }
    }

}

 

๋ฒ„ํŠผ ํด๋ฆญ or ์˜†์œผ๋กœ ์Šฌ๋ผ์ด๋“œ์‹œ ํ™”๋ฉด์ด ๋‚˜์˜ค๊ณ  back key ๋ˆŒ๋ €์„ ๊ฒฝ์šฐ ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ณ„๋„๋กœ ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ ์šฉ ์‹œํ‚ค์ง€ ์•Š๊ณ ๋„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์Šฌ๋ผ์ด๋“œ ๋ฉ”๋‰ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

[๊ฒฐ๊ณผํ™”๋ฉด]