简体   繁体   English

具有不同活动或片段的导航抽屉

[英]Navigation drawer with different activities or fragments

I will be starting with a, new app development.As i am new to android development, i wanted a suggestion.In my app i will be using a navigation drawer.Now i am confused that whether to use different activities or fragments.Also i wanted to make a navigation drawer common to all the activites without written the drawer code in all the activities.Please can some one help me which is the better way 我将开始一个新的应用程序开发。由于我是android开发的新手,我想要一个建议。在我的应用程序中,我将使用导航抽屉。现在我很困惑是否使用不同的活动或片段。想要使导航抽屉对所有活动都通用,而无需在所有活动中编写抽屉代码。请有人帮我,这是更好的方法

  1. To use one activities and multiple fragments or 使用一项活动和多个片段,或者
  2. one activity associated with a fragments. 与片段相关的一项活动。
  3. Also how to use common navigation drawer code(along with actionBar) in all the activities 以及如何在所有活动中使用通用导航抽屉代码(以及actionBar)

One way you can do it is having a BaseActivity that extends ActionBarActivity and has the DrawerLayout and a FrameLayout that serves as a container to your current Fragment . 一种实现方法是使用BaseActivity扩展ActionBarActivity并具有DrawerLayoutFrameLayout作为当前Fragment的容器。 It also has the code to put the DrawerLayout functioning and has an abstract method that changes the Fragment inside it. 它还具有放置DrawerLayout功能的代码,并具有更改其中的Fragment的抽象方法。 Like this: 像这样:

public abstract void putFragment();

inside onCreate() method, the last call you make is putFragment() ; onCreate()方法中,您最后一次调用是putFragment()

Then you will have as many Activities you want that will extend this BaseActivity and will implement putFragment() method. 然后,您将拥有想要的活动,这些活动将扩展此BaseActivity并实现putFragment()方法。 What they will do there is change the Fragment in the FrameLayout container. 他们将在FrameLayout容器中更改Fragment It means you have one Fragment for each sub Activity of BaseActivity . 这意味着BaseActivity每个子Activity都有一个Fragment

You do not need to implement onCreate() method in your subActivities, because they inherit the parent Activity layout. 您无需在subActivities中实现onCreate()方法,因为它们继承了父Activity布局。 Just implement putFragment() method so you have control on the current Fragment. 只需实现putFragment()方法,您就可以控制当前的Fragment。 Inside onCreateView() of each Fragment, do your app logic. 在每个Fragment的onCreateView()中,执行您的应用程序逻辑。

About the DrawerLayout, you just have to implement a CallBack in each of your SubActivities to handle a click in the DrawerLayout , and then execute a method like selectDrawerItem() , that starts the desired Sub Activity . 关于DrawerLayout,您只需要在每个SubActivity中实现一个CallBack来处理DrawerLayout的单击,然后执行诸如selectDrawerItem()类的方法selectDrawerItem()启动所需的Sub Activity

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

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