๐ป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 ๋๋ ์ ๊ฒฝ์ฐ ์ฌ๋ผ์ด๋ ๋ฉ๋ด๊ฐ ์ฌ๋ผ์ง๋ ๋ถ๋ถ๊น์ง ์ ์ฉํ์ต๋๋ค. ์์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด ๋ณ๋๋ก ์ฌ๋ผ์ด๋ ๋ฉ๋ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ์ฉ ์ํค์ง ์๊ณ ๋ ๊ฐ๋จํ๊ฒ ์ฌ๋ผ์ด๋ ๋ฉ๋ด๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
[๊ฒฐ๊ณผํ๋ฉด]