DrawLayout 몇 가지 주의 점

package com.example.navigationdrawer5;

import java.util.Locale;

import android.app.Activity;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class DrawerActivity extends Activity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView drawerList;
private String[] planetTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
planetTitles = getResources().getStringArray(R.array.planets_array);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerList = (ListView) findViewById(R.id.drawerList);
drawerList.setAdapter(new ArrayAdapter(this, R.layout.drawer_list_item, planetTitles));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
selectItem(position);
}
});
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
int position = drawerList.getCheckedItemPosition();
getActionBar().setTitle(planetTitles[position]);
invalidateOptionsMenu();
super.onDrawerClosed(drawerView);
}

@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}

private void selectItem(int position) {
Fragment fragment = new DrawerFragment();
Bundle args = new Bundle();
args.putInt(DrawerFragment.ARG_PLANET_ID, position);
fragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.contentFrame, fragment).commit();
drawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerList);
getActionBar().setTitle(planetTitles[position]);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//5、
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean isDrawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_search).setVisible(!isDrawerOpen);
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//6. action bar item
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, " ", Toast.LENGTH_LONG).show();
}
break;
}
return super.onOptionsItemSelected(item);
}

public static class DrawerFragment extends Fragment {
private static final String ARG_PLANET_ID = "planet_id";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView view = (ImageView) inflater.inflate(R.layout.drawer_content, container, false);
Resources res = getActivity().getResources();
int planetId = getArguments().getInt(ARG_PLANET_ID);
int imageid = res.getIdentifier(res.getStringArray(R.array.planets_array)[planetId].toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
view.setImageResource(imageid);
return view;
}
}

}

android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>




android:id="@+id/drawerList"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"/>


좋은 웹페이지 즐겨찾기