控制与NavigationDrawer工具栏图标 [英] Control the Toolbar icon with NavigationDrawer

查看:254
本文介绍了控制与NavigationDrawer工具栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在我的应用程序的 android.support.v7.widget.Toolbar android.support.v4.widget.DrawerLayout 。一切工作的权利,但有一个轻微的事情,我想改变自己的行为。

I'm currently using within my app the android.support.v7.widget.Toolbar and android.support.v4.widget.DrawerLayout. Everything is working right, but there is a slight thing I want to change its behaviour.

当我打开抽屉,将整个文件柜占用工具栏的的空间。这将是很好的工具栏停留在水面上,就像在 谷歌音乐应用程序 。我怎样才能做到这一点?

When I open the drawer, the whole drawer occupies the space of the Toolbar. It would be nice that the Toolbar stays on top, like in the Google Music app. How can I achieve that?

但最重要的是不要在previous。 首先,这是在应用程序加载的图标是三少了一份。我已经意识到,打开抽屉后,图标会变为箭头。和加载片段后,箭头仍存在的工具栏图标,即使我preSS回来,直到第一个画面。我怎么能避免打开抽屉后出现的箭头?我想手动更改该图标,特别是当我打开低级别的碎片。

But the most important thing isn't the previous. At first, the icon which is loaded in the application is the three stripped one. I've realised that after opening the drawer, the icon changes to an arrow. And after loading a fragment, the arrow remains there as the Toolbar icon, even if I press back until the first screen. How could I avoid the arrow appearing from after opening the drawer? I'd want to change this icon manually, specially when I load lower level fragments.

感谢您的帮助!

code:

public class HomeActivity extends BaseActivity {



...

    private DrawerLayout mDrawer;
    private ActionBarDrawerToggle mDrawerToggle;
    private ListView mDrawerList;
    private ListView mDrawerRightList;
    private RelativeLayout mDrawerRelativeLayout;
    private String[] mDrawerMenuTitles;
    private Toolbar mToolbar;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mDrawerMenuTitles = getResources().getStringArray(R.array.main_menu_options);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer);

        mDrawerRelativeLayout = (RelativeLayout) findViewById(R.id.theDrawerRelativeLayout);

        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mDrawerRightList = (ListView) findViewById(R.id.theDrawerRight);
        mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerRightList);

        mDrawerList = (ListView) findViewById(R.id.theDrawer);

        mDrawerList.setAdapter(new ArrayAdapter<String>(
                getSupportActionBar().getThemedContext(), 
                R.layout.drawer_list_item,
                mDrawerMenuTitles
        ));

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

        setmToolbar((Toolbar) findViewById(R.id.toolbar));

        mDrawerToggle = new ActionBarDrawerToggle(
                this, 
                mDrawer,
                mToolbar,
                R.string.drawer_open, 
                R.string.drawer_close){

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                // TODO Auto-generated method stub
                super.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                //getSupportActionBar().setTitle(CURRENT_FRAGMENT);
            }

            /** Called when a drawer has settled in a completely open state. */
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                //getSupportActionBar().setTitle("Configuración");
            }           

        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerToggle.syncState();
        mDrawer.setDrawerListener(mDrawerToggle);

        setToolbarSubtitle(getString(R.string.misrutas_titulo));

        initialisePreferences(savedInstanceState);

        if (savedInstanceState == null) {
            replaceFragment(DEFAULT_FRAGMENT);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawer.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

     private class DrawerItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //selectItem(position);
            final int thePos = position;
            mDrawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                ....
                }
            });

            if(mDrawer.isDrawerOpen(GravityCompat.START))
                mDrawer.closeDrawer(mDrawerRelativeLayout);

        }
    }

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

     @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onBackPressed(){
        setToolbarSubtitle(getString(R.string.app_name_subtitle));
      if (getSupportFragmentManager().getBackStackEntryCount() == 1){
        finish();
      }
      else {
        super.onBackPressed();
      }
    }

    private void replaceFragment (String to){

        if(!to.equalsIgnoreCase(CURRENT_FRAGMENT)){
            CURRENT_FRAGMENT = to;
            Fragment fragment = Fragment.instantiate(HomeActivity.this, to);
            String backStateName =  fragment.getClass().getName();
            String fragmentTag = backStateName;

            FragmentManager manager = getSupportFragmentManager();
            boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);

            if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null){ //fragment not in back stack, create it.
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.content_frame, fragment, fragmentTag);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(backStateName);
                ft.commit();
            } 

        }

    }

    //More methods
    ....

}

编辑:

我已经能够改变一点点我的布局,从而使工具栏停留在剩下的屏幕(抽屉和片段)的顶部。但我一直无法控制的图标。当我打开抽屉,汉堡包图标转换成箭头。当我关闭抽屉时,箭头被转换成汉堡包。但是,当我preSS的所述抽屉中的一个选项,一个新片段被充气和汉堡包图标替换为箭头,直到应用程序被关闭,以使汉堡包图标是从未见过。

I have been able to modify a little bit my layout, so that the Toolbar stays on top of the rest screen (drawer and fragment). But I haven't been able to control the icon. When I open the drawer, the hamburger icon converts into the arrow. When I close the drawer, the arrow is converted into the hamburger. But when I press an option within the drawer, a new fragment is inflated and the hamburger icon is replaced by the arrow until the application is closed, so that the hamburger icon is never seen.

我如何能适应的图标行为,使其不会从汉堡包变成箭头,当我点击了一抽屉的选择吗?

How can I adapt the icon behaviour so that it doesn't change from hamburger to arrow when I click over a drawer option?

推荐答案

乍一看似乎你在这个片段中设置项目时,选择新的抽屉式监听器,precisely:

At first glance seems that you are setting new drawer listener whenever item is selected, precisely in this snippet:

 private class DrawerItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //selectItem(position);
            final int thePos = position;
            mDrawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                @Override
                public void onDrawerClosed(View drawerView){
                ....
                }
            });

            if(mDrawer.isDrawerOpen(GravityCompat.START))
                mDrawer.closeDrawer(mDrawerRelativeLayout);

        }
    }

这个片段替换 ActionBarDrawerToggle (实现 DrawerListener 也一样),所以没有调用来ActionBarDrawerToggle控制该图标。

This snippet replaces ActionBarDrawerToggle (which implements DrawerListener as well), so no calls are made to ActionBarDrawerToggle which controls the icon.

如何调整图标的行为,以便它不会从汉堡包变成箭头,当我点击了一抽屉的选择吗?

与您previous声明,这声音模糊了我。为什么你会在preventing此默认行为?您可以通过不调用 DrawerListener 的方法调用超prevent变形。

Relating to your previous statements, this sound vague to me. Why you'd be preventing this default behaviour? You can prevent morphing by not calling super calls in DrawerListener's methods.

这篇关于控制与NavigationDrawer工具栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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