导航抽屉切换不会更改状态

时间:2015-08-28 10:10:35

标签: android navigation-drawer

我开始一个新项目,只需从旧项目中复制/粘贴代码,一切正常。在这一个切换按钮不会改变状态(它只是箭头,当我按下它时不工作),如:

enter image description here

enter image description here

使用滑动触摸打开/关闭抽屉工作正常

必须注意我只使用了一个具有布局的Activity:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/base_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<RelativeLayout
    android:id="@+id/base_container_for_fragments"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    //elements for screen

</RelativeLayout>


<!--that is just for drawer-->
<ListView
    android:id="@+id/navigationDrawerList"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="@color/color_red_style" />

在我的活动中:

//声明变种:

private ListView mDrawerList;
protected DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
onCreate()中的

   dataList = new ArrayList<DrawerItem>();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.base_layout);
    mDrawerList = (ListView) findViewById(R.id.navigationDrawerList);

    addDrawerItems();
    setupDrawer();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

addDrawerItems()工作正常

 protected void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer, R.string.close_drawer) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle(getResources().getString(R.string.bar_menu));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle("produse");


            // creates call to onPrepareOptionsMenu()
            invalidateOptionsMenu();
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

当然我在onOptionsItemSelected(MenuItem item)

中放了句柄切换
 // Activate the navigation drawer toggle
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

那么问题是什么,我无法实现......任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

omg我忘了同步按钮切换状态:)

我必须在MainActivity()

中添加此内容
@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }