[英]Action Bar, Navigation drawer and fragment backstack management
I'm implementing the following tutorial in my app: Creating a Navigation Drawer . 我正在我的应用程序中实现以下教程: 创建导航抽屉 。
My target is to implement the following behavior: 我的目标是实现以下行为:
How can I implement that transformation of the home button? 如何实现主页按钮的转换?
Thanks 谢谢
There are some methods in the ActionBar class that may help you: ActionBar类中有一些方法可以帮助您:
The following call will set the Home button icon to the "back" icon. 以下调用会将“主页”按钮图标设置为“后退”图标。
getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
Because the MenuItem's action is defined by onOptionsItemSelected() method, so you have to override it too, and check whether you click the home button or not. 因为MenuItem的动作是由onOptionsItemSelected()方法定义的,所以您也必须覆盖它,并检查是否单击home按钮。 Actually, the navigation drawer will draw again so you will want to return true from that method to prevent the drawer to draw again.
实际上,导航抽屉将再次绘制,因此您将希望从该方法返回true,以防止抽屉再次绘制。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getId() == android.R.id.home) {
// do what you want here
return true; // prevent the drawer to draw again;
}
return super.onOptionsItemSelected(item);
}
To turn the home button back to its default "behavior", use the following call: 要将主按钮恢复为默认的“行为”,请使用以下调用:
getActionBar().setDisplayShowHomeEnabled(true);
The last step is to check if your current fragment is the FragmentA (which will active the drawer draw the navigation fragment) or the FragmentB (which does not). 最后一步是检查您当前的片段是FragmentA(它将激活抽屉绘制导航片段)还是FragmentB(不是)。 You may want to add a global boolean variable to do them, and check the position of the navigation item.
您可能需要添加一个全局布尔变量来执行它们,然后检查导航项的位置。
I have included an example here, please check out: 我在这里提供了一个示例,请查看:
Navigation Drawer example 导航抽屉示例
Hope this help. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.