1.import project actionbarsherlock as library project.
2.creat a new project ,right-click the project choose properties->android add actionbarsherlock as library.
3.create a Drawer Layout
To add a navigation drawer, declare your user interface with a
DrawerLayout
object as the root view of your layout. Inside the DrawerLayout
, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
For example, the following layout uses a
DrawerLayout
with two child views: a
FrameLayout
to contain the main content (populated by a
Fragment
at runtime), and a
ListView
for the navigation drawer.
<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">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="8dip"
android:padding="12dip"
android:listSelector="@android:color/transparent"
android:scrollingCache="false"
android:fadingEdge="none"
android:scrollbars="none"
android:background="@drawable/left_nav_title_bg" />
</android.support.v4.widget.DrawerLayout>
4.create MainActibity inherit
SherlockFragmentActivity.
public class MainActivity extends SherlockFragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
private SimpleAdapter mAdapter;
private int mTag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array );
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout );
mDrawerList = (ListView) findViewById(R.id.left_drawer );
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat. START);
// set up the drawer's list view with items and click listener
mAdapter = new SimpleAdapter(this, getData(),
R.layout. drawer_list_item, new String[] { "text" },
new int [] { R.id.text, }) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == mTag ) {
view.setBackgroundResource(R.drawable. left_nav_channel_selected);
} else {
view.setBackgroundResource(R.drawable. list_item_selector);
}
return view;
}
};
mDrawerList.setAdapter(mAdapter );
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled( true);
getSupportActionBar().setHomeButtonEnabled( true);
getSupportActionBar().setLogo(R.drawable. device_access_storage);
/*
* To listen for drawer open and close events, call setDrawerListener()
* on your DrawerLayout and pass it an implementation of
* DrawerLayout.DrawerListener. This interface provides callbacks for
* drawer events such as onDrawerOpened() and onDrawerClosed().
*/
mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerStateChanged(int arg0) {
}
@Override
public void onDrawerSlide(View arg0, float arg1) {
}
/** Called when a drawer has settled in a completely closed state. */
@SuppressLint("NewApi" )
@Override
public void onDrawerOpened(View arg0) {
getSupportActionBar().setTitle( mDrawerTitle);
invalidateOptionsMenu(); //creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
@SuppressLint("NewApi" )
@Override
public void onDrawerClosed(View arg0) {
getSupportActionBar().setTitle( mTitle);
invalidateOptionsMenu(); //creates call to onPrepareOptionsMenu()
}
});
if (savedInstanceState == null) {
selectItem(0);
}
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < mPlanetTitles. length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put( "text", mPlanetTitles [i]);
list.add(map);
}
return list;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList );
menu.getItem(0).setVisible(!drawerOpen);
return super .onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add( "Search")
.setIcon(R.drawable. action_search)
.setShowAsAction(
MenuItem. SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT );
return true ;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/*when click the app icon change the drawer state*/
if(item.getItemId() == android.R.id.home){
changeDrawerState();
return true ;
}
if (item.getTitle().equals("Search" )) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager. QUERY, getSupportActionBar()
.getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast. makeText(this, R.string.app_not_available ,
Toast. LENGTH_LONG).show();
}
return true ;
}
return super .onOptionsItemSelected(item);
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
mTag = position;
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment. ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = this.getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id. content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle( mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList );
}
private void changeDrawerState() {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList );
if (drawerOpen) {
/*close the drawer*/
mDrawerLayout.closeDrawer(mDrawerList );
} else {
/*open the drawer*/
mDrawerLayout.openDrawer(mDrawerList );
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle( mTitle);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet ,
container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER );
String planet = getResources()
.getStringArray(R.array. planets_array)[i];
((TextView) rootView.findViewById(R.id.image )).setText(planet);
getActivity().setTitle(planet);
return rootView;
}
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
changeDrawerState();
}
return super .onKeyUp(keyCode, event);
}
}