导航抽屉开关的活动,而不是片段 [英] Navigation Drawer to switch activities instead of fragments

查看:134
本文介绍了导航抽屉开关的活动,而不是片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用的导航抽屉的android,但是,而不是更新的片段,我想为我的手段在应用程序内航行活动之间进行切换。

Is it possible to use a navigation drawer in android but instead of updating fragments, i would like to switch between activities as my means of navigation within the app.

推荐答案

是的,它是可能的 - 这就是我所做的我的应用程序。我已经有一定数量的设立活动,而不是将它们全部转换为碎片,我想定制的导航抽屉跨越所有这些工作。不幸的是,这不是一个快速的解决方法,所以如果你有使用片段的选择,我会与该走了。但不管这里的我是如何做到的:

Yes it is possible - it's what I did for my app. I already had a number of activities set up, and rather than convert them all to fragments, I wanted to tailor the navigation drawer to work across all of them. Unfortunately, it's not a quick workaround, so if you have the option of using fragments, I would go with that. But regardless here's how I did it:

让我们说我有2个活动,这两个我想有导航抽屉。在layout.xml每个,我指定了 DrawerLayout 用适当的列表视图来握住我的导航选项。从本质上讲,导航抽屉是由我每次活动之间切换,给人的外观,它是坚持。为了让生活变得更加简单,我把设置导航抽屉,把他们自己的类所需的常用方法: NavigationDrawerSetup.java 。这样我的活动可以使用相同的定制适配器等

Let's say I have 2 activities, both of which I want to have the Navigation Drawer. In the layout.xml for each, I specified a DrawerLayout with the appropriate list view to hold my navigation options. Essentially, the Navigation drawer is made every time I switch between activities, giving the appearance that it is persisting. To make life a lot easier, I took the common methods required to set up the navigation drawer and put them in their own class: NavigationDrawerSetup.java. That way my activities can use the same custom adapter, etc.

在此 NavigationDrawerSetup.java 类,我有以下几点:

Within this NavigationDrawerSetup.java class, I have the following:

  • configureDrawer() - 此设置的动作条, ActionBarDrawerToggle,和所需的听众
  • 在我的自定义阵列适配器(填充列表内的导航选项)
  • 的selectOptions()方法,该方法
  • configureDrawer() - this sets up the ActionBar, ActionBarDrawerToggle, and the required listeners
  • My custom array adapter (to populate the navigation options within the list)
  • The selectOptions() method, which

当您设置导航抽屉内的活动之一,您只需创建一个新的navigationDrawer对象,并传入所需的布局参数(如drawerLayout,ListView控件等)。然后你会打电话 confiugreDrawer()

When you set up the navigation drawer within one of your activities, you just create a new navigationDrawer object and pass in the required layout parameters (like the drawerLayout, listView etc). Then you'd call confiugreDrawer():

        navigationDrawer = new NavigationDrawerSetup(mDrawerView, mDrawerLayout,
            mDrawerList, actionBar, mNavOptions, currentActivity);

    navigationDrawer.configureDrawer();

currentActivity 是因为导航抽屉被绑定到你的活动来传递。你将不得不使用它,当你设置了ActionBarDrawerToggle:

currentActivity is passed in since the navigation drawer is tied to the activity you are on. You will have to use it when you set up the ActionBarDrawerToggle:

mDrawerToggle = new ActionBarDrawerToggle(currentActivity, // host Activity
        mDrawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description for accessibility */
        R.string.drawer_close /* "close drawer" description for accessibility */
        )

seeting你arrayAdapter时,您还需要使用currentActivity:

You will also need to use currentActivity when seeting up your arrayAdapter:

至于如何通过导航抽屉活动之间进行切换,你可以建立新的意图你的选择信息中()方法:

As for how to switch between activities via the navigation drawer, you can just set up new intents within your selectItem() method:

private void selectItem(int position) {

    // Handle Navigation Options
    Intent intent;
    switch (position) {
        case 0:
                intent = new Intent(currentActivity, NewActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                currentActivity.startActivity(intent);
            }
            break;
        case 1: // etc.

只要确保你的NewActivity也有抽屉式导航栏的设置,它应该显示。

Just make sure that your NewActivity also has the navigation drawer setup and it should display.

有一吨的东西,你可以做到这一点的方法来定制你自己的需要,但是这是我做到了的一般结构。希望这有助于!

There are a ton of things you can do to customize this method to your own needs, but this is the general structure of how I did it. Hope this helps!

这篇关于导航抽屉开关的活动,而不是片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆