简体   繁体   English

了解android导航抽屉和片段

[英]Understanding android navigation drawer and fragments

i'm quite new to drawer material and i have trouble understanding some things: I need to create an Activity with a Fragment on it. 我是抽屉材料的新手,在理解某些内容时遇到了麻烦:我需要创建一个带有FragmentActivity Different selections on the Drawer must replace the current Fragment with another one, but is the Drawer something in the fragment or it is one for the activity itself. Drawer上的不同选择必须用另一个替换当前的Fragment ,但是Drawer是片断中的某物,还是活动本身的一个。 More specifically is the Drawer living in the Fragment and if not is it possible to create it in the Fragment . 更具体地说, Drawer位于Fragment ,如果没有,则可以在Fragment创建它。 I am asking this cause i see that when initiating the Drawer you need to fill parent activity. 我问这个原因,因为我看到启动Drawer时需要填写父级活动。 Also when i tried to use the Navigation Drawer template in Android Studio i didn't had the Use a Fragment checkbox. 另外,当我尝试在Android Studio使用Navigation Drawer模板时,我没有Use a Fragment复选框。

There are many ways to achieve what you want. 有很多方法可以实现您想要的。 But I think the simplest way is: 但我认为最简单的方法是:

1) DrawerLayout view should reside in the activity (probably as the base layout). 1) DrawerLayout视图应该驻留在活动中(可能作为基本布局)。

2) When you click on an item in the draw 2 things happen: 2)当您在抽奖中单击一个项目时,会发生两种情况:

  • The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it). 片段被替换(您有一个布局来包含片段,而您只需替换其中的片段)。

  • The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged() . 抽屉中的项目会更新(如果要创建列表,则只需设置数据并调用notifyDataSetChanged()

Don't forget to save your state so it can recover in case the Activity is recreated. 不要忘记保存状态,以便在重新创建Activity时可以恢复状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM