<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">
<!-- The main content view -->
...
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#EEA9B8"/>
</android.support.v4.widget.DrawerLayout>
/** 抽屉导航变量 */
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
...
/** 抽屉导航相关 */
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.draw, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);
mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(false),
R.layout.drawer_item, new String[] { "drawer_img",
"drawer_title" }, new int[] { R.id.drawer_img,
R.id.drawer_title }));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(
this, /** host Activity */
mDrawerLayout, /** DrawerLayout object */
R.drawable.draw, /** nav drawer p_w_picpath to replace 'Up' caret */
R.string.search_label, /** "open drawer" description for accessibility */
R.string.defalut_text /** "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("onDrawerClosed");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("onDrawerOpened");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
...
/**
* 监听抽屉导航item点击事件
*/
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(view, position);
}
}
private boolean flag = false;
private void selectItem(View view, int position) {
if (position == 0) {
if (!flag) {
mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(true),
R.layout.drawer_item, new String[] { "drawer_img",
"drawer_title" }, new int[] { R.id.drawer_img,
R.id.drawer_title }));
flag = true;
} else {
mDrawerList.setAdapter(new SimpleAdapter(this, getDrawerItems(false),
R.layout.drawer_item, new String[] { "drawer_img",
"drawer_title" }, new int[] { R.id.drawer_img,
R.id.drawer_title }));
flag = false;
}
} else {
TextView title = (TextView) view.findViewById(R.id.drawer_title);
String cityName = title.getText().toString();
this.cityInput.setText(cityName);
}
} |