How can I dismiss the Navigation Drawer to use the Back-home-icon button?

梦想的初衷 提交于 2019-12-05 20:56:29

问题


I'm using the action bar with search bar too, and I need to use the ActionBar ico like a Back button:

But I'm using the navigation drawer too... How can I dismiss/hide/disable the Navigation Drawer menu to use the back button?

My ActionBar code:

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    this.getSupportActionBar().setDisplayShowCustomEnabled(true);
    this.getSupportActionBar().setDisplayShowTitleEnabled(false);

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

    LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v;

    if(!searchView){
        v = inflator.inflate(R.layout.action_textview, null);
        ((TextView) v.findViewById(R.id.titleText)).setText(actionTitle);
        menu.getItem(0).setVisible(true);
        menu.getItem(2).setVisible(true);
        mainMenu = menu;

    }else{
        v = inflator.inflate(R.layout.action_searchview, null);
        actionSearch = (EditText) v.findViewById(R.id.searchText);
        actionSearch.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
                int result = actionId & EditorInfo.IME_MASK_ACTION;
                switch(result) {
                case EditorInfo.IME_ACTION_DONE:
                case EditorInfo.IME_ACTION_NEXT:
                case EditorInfo.IME_ACTION_GO:
                case 0:
                    ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(actionSearch.getWindowToken(), 0);
                    String temp = actionSearch.getText().toString();
                    searchFor(temp);
                    break;
                }
                return true;
            }
        });
        actionSearch.requestFocus();
        menu.getItem(0).setVisible(false);
        menu.getItem(2).setVisible(false);
    }
    this.getSupportActionBar().setCustomView(v);        

    return super.onCreateOptionsMenu(menu);
}

Code help:

  1. My Title its a custom view with only a TextView, to customize the font color and size; (action_textview)
  2. My SearchBar use a custom view with only a EditText; (action_searchview)

回答1:


From ActionBarDrawerToggle you can use the method setDrawerIndicatorEnabled(Boolean enable). Alternatively, you can set the ActionBar Display options. Specifically the flag DISPLAY_HOME_AS_UP. Link to docs

Then handle the click event as usual.



来源:https://stackoverflow.com/questions/19660652/how-can-i-dismiss-the-navigation-drawer-to-use-the-back-home-icon-button

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