先看看效果
使用最新的android-support-v4.jar
- <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">
-
- ...
-
- <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,
- mDrawerLayout,
- R.drawable.draw,
- R.string.search_label,
- R.string.defalut_text
- ) {
- public void onDrawerClosed(View view) {
- getActionBar().setTitle("onDrawerClosed");
- invalidateOptionsMenu();
- }
-
- public void onDrawerOpened(View drawerView) {
- getActionBar().setTitle("onDrawerOpened");
- invalidateOptionsMenu();
- }
- };
- mDrawerLayout.setDrawerListener(mDrawerToggle);
-
- ...
-
-
-
-
- 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);
- }
- }