これを試して :
これを main_activity.xml に追加します。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<ListView
android:id="@+id/drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#F3F3F4"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
/>
</android.support.v4.widget.DrawerLayout>
これを Main_Activity.java に追加します。
public class MainActivity extends Activity{
private ListView listView;
private DrawerLayout drawerLayout;
private MyActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
listView = (ListView) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
MenuAdapter adapter = new MenuAdapter(this);
adapter.add(new MenuModel("Title Menu", 0, 0, true));
adapter.add(new MenuModel("Food",R.drawable.your_drawable, 43, false));
adapter.add(new MenuModel("Beverage", R.drawable.your_drawable, 17, false));
adapter.add(new MenuModel("Juice",R.drawable.your_drawable, 10, false));
adapter.add(new MenuModel("Snack",R.drawable.your_drawable, 7, false));
adapter.add(new MenuModel("Other", 0, 0, true));
adapter.add(new MenuModel("Setting",R.drawable.action_settings, 0, false));
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if(arg2 == 1){
Intent i = new Intent(MainActivity.this, Food.class);
startActivity(i);
} else if(arg2 == 2){
Intent i = new Intent(MainActivity.this, Beverage.class);
startActivity(i);
} else if(arg2 == 3){
Intent i = new Intent(MainActivity.this, Juice.class);
startActivity(i);
} else if(arg2 == 4){
Intent i = new Intent(MainActivity.this, Snack.class);
startActivity(i);
} else if(arg2 == 6){
Intent i = new Intent(MainActivity.this, Gallery_Menu_Paket_Active.class);
startActivity(i);
}
}
});
drawerToggle = new MyActionBarDrawerToggle(this, drawerLayout);
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
private class MyActionBarDrawerToggle extends ActionBarDrawerToggle{
public MyActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
super(mActivity,mDrawerLayout,R.drawable.ic_drawer,R.string.app_name,R.string.app_name);
}
@Override
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
public class MenuAdapter extends ArrayAdapter<MenuModel> {
public MenuAdapter(Context context) {
super(context, 0);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return getItem(position).isGroupTitle() ? 0 : 1;
}
@Override
public boolean isEnabled(int position) {
return !getItem(position).isGroupTitle();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MenuViewHolder holder = null;
int type = getItemViewType(position);
MenuModel menu= getItem(position);
if (convertView == null) {
switch (type) {
case 0:
convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row_group, null);
holder = new MenuViewHolder((TextView) convertView.findViewById(R.id.group_title),null, null);
break;
case 1:
convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);
holder = new MenuViewHolder(
(TextView) convertView.findViewById(R.id.menu_title),
(ImageView) convertView.findViewById(R.id.row_icon),
(TextView) convertView.findViewById(R.id.row_counter));
break;
}
convertView.setTag(holder);
} else {
holder = (MenuViewHolder) convertView.getTag();
}
holder.titleView.setText(menu.getTitle());
if(type != 0){
holder.iconView.setImageResource(menu.getIcon());
if (menu.getCounter() > 0) {
holder.counterView.setVisibility(View.VISIBLE);
holder.counterView.setText(menu.getCounter() + "");
} else {
holder.counterView.setVisibility(View.GONE);
}
}
return convertView;
}
public class MenuViewHolder {
public final TextView titleView;
public final ImageView iconView;
public final TextView counterView;
public MenuViewHolder(TextView titleView, ImageView iconView,
TextView counterView) {
this.titleView = titleView;
this.iconView = iconView;
this.counterView = counterView;
}
}
}
public class MenuModel {
private String title;
private int icon;
private int counter;
private boolean isGroupTitle;
public MenuModel(String title, int icon, int counter, boolean isGroupTitle) {
this.title = title;
this.icon = icon;
this.counter = counter;
this.isGroupTitle = isGroupTitle;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public boolean isGroupTitle() {
return isGroupTitle;
}
public void setGroupTitle(boolean isGroupTitle) {
this.isGroupTitle = isGroupTitle;
}
}
}