Drawer toggle not working as expected (icon remains the same)

烈酒焚心 提交于 2019-12-02 21:21:12

问题


I'm trying to implement an ActionBarDrawerToggle into my app but I can't make it. I've achieved to show the toggle in my toolbar, but the icon is the same always.

This is toggle's icon when drawer is closed:

http://i.stack.imgur.com/HTcom.png

And this is when drawer is opened:

http://i.stack.imgur.com/dX0Z2.png

As you can see, it doesn't change from ic_drawer to back arrow. ic_drawer is never shown.

So, here's my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    initializeDrawer();
    populateDrawer();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

private void initializeDrawer() {
        tagTitles = getResources().getStringArray(R.array.item_names);
        icons = getResources().getStringArray(R.array.item_icons);
        dwLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        dwList = (ListView) findViewById(R.id.drawer_list);
        dwList.setOnItemClickListener(new DrawerItemClickListener());
        dwToggle = new ActionBarDrawerToggle(this, dwLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle("pepe");
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle("pop");
                invalidateOptionsMenu();
            }
        };
        dwLayout.setDrawerListener(dwToggle);
    }

    private void populateDrawer() {
        ArrayList<DrawerItem> dwItems = new ArrayList<>();
        for (int i = 0; i < tagTitles.length; i++) {
//ignore this; population is not properly working
            dwItems.add(new DrawerItem(tagTitles[i], Resources.getSystem().getIdentifier("abc_ic_menu_copy_mtrl_am_alpha.png", "drawable", getBaseContext().getPackageResourcePath())));
        }

        dwList.setAdapter(new NavigationDrawerAdapter(this, dwItems));
    }

@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    dwToggle.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (dwToggle.onOptionsItemSelected(item)) {
        return true;
    }

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

Thanks!


回答1:


implement following method with DrawerLayout instace, in your case instance is dwLayout. implement following after this line

dwLayout.setDrawerListener(dwToggle);

add this

drawerLayout.post(new Runnable() {
    @Override
    public void run() {
        // To display hamburger icon in toolbar
        drawerToggle.syncState();
    }
});

or you can do this as well

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}


来源:https://stackoverflow.com/questions/30490161/drawer-toggle-not-working-as-expected-icon-remains-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!