[英]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开发的新手,我想要一个建议。在我的应用程序中,我将使用导航抽屉。现在我很困惑是否使用不同的活动或片段。想要使导航抽屉对所有活动都通用,而无需在所有活动中编写抽屉代码。请有人帮我,这是更好的方法
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
并具有DrawerLayout
和FrameLayout
作为当前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.