抽屉切换工作不正常(图标保持不变) [英] Drawer toggle not working as expected (icon remains the same)

查看:235
本文介绍了抽屉切换工作不正常(图标保持不变)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个ActionBarDrawerToggle到我的应用程序,但我不能做到这一点。我已经取得了展现在我的工具栏上的切换,但是图标是一样的始终。

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

这是当抽屉被打开:

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

正如你所看到的,它不会从ic_drawer改变回头箭。 ic_drawer永远不会显示。

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);
    }
}

谢谢!

推荐答案

实现与DrawerLayout instace下面的方法,你的情况实例dwLayout。实现这一行之后下

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

dwLayout.setDrawerListener(dwToggle);

添加此

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();
}

这篇关于抽屉切换工作不正常(图标保持不变)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆