First of all, about Drawerlayout, he is a view that can be used to make a menu, where his first view is the content area, and then he can have two view, depending on whether their Layout_gravity property is right or left menu, The left side is equal to right, which is the side menu. The General menu is a ListView.
We used toolbar instead of the original actionbar to let him use it with drawerlayout. Click on the toolbar of the navigation icon can show or hide the menu, as well as the effect, similar to the app, then there is a problem, when we click on the Display menu, if you click the Return button, then he will launch the application instead of closing the menu, this is not the result we want, There are two ways to solve it.
One, we are through actionbardrawertoggle to listen to the open shutdown of drawerlayout, so we can rewrite actionbardrawertoggleondrawerclosed and Ondraweropend methods , and then override the OnKeyDown method to set a flag. The specific code is as follows:
Mactionbardrawertoggle = new Actionbardrawertoggle (this, mdrawerlayout, Mtoolbar, R.string.open, R.string.close) {@Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (drawerview); flag = false;} @Overridepublic void ondraweropened (View drawerview) {super.ondraweropened (drawerview); flag = True;}}; Mactionbardrawertoggle.syncstate (); Mdrawerlayout.setdrawerlistener (Mactionbardrawertoggle);
public boolean onKeyDown (int keycode, keyevent event) { if (flag) { mdrawerlayout.closedrawers (); return true; } Return Super.onkeydown (KeyCode, event);}
This will solve the problem.
The second solution, because our left menu (mine) is made using fragment, can be achieved through his hid and show methods.
The specific code is as follows:
Mactionbardrawertoggle = new Actionbardrawertoggle (this, mdrawerlayout, Mtoolbar, R.string.open, R.string.close) {@Overridepublic void ondrawerclosed (View drawerview) {super.ondrawerclosed (drawerview); flag = false;} @Overridepublic void ondraweropened (View drawerview) {super.ondraweropened (Drawerview); Fragmentmanager fm = Getsupportfragmentmanager (); Fm.begintransaction (). Show (Mleftmenufragment). commit (); flag = True;}}; Mactionbardrawertoggle.syncstate (); Mdrawerlayout.setdrawerlistener (Mactionbardrawertoggle);
public boolean onKeyDown (int keycode, keyevent event) { if (flag) { Fragmentmanager fm = Getsupportfragmentmanager (); Fm.begintransaction (). Hide (Mleftmenufragment). commit (); return true; } Return Super.onkeydown (KeyCode, event);}
The first one is recommended, the second kind of personal test will have a lag, click the Back button and then click the Menu button, the menu content will appear for a while, the first kind will not.
Drawerlayout + Toolbar The solution of closing the application instead of the menu by pressing the return key.