如何在没有片段的情况下使用导航抽屉? [英] How can I use the Navigation Drawer without fragments?

查看:17
本文介绍了如何在没有片段的情况下使用导航抽屉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照此教程了解如何创建一个导航抽屉,但我不想在用户从抽屉列表中选择一个项目后使用片段来显示新内容.解决此问题的最佳方法是什么?我正在使用不实现 Fragments 的 API 10.

解决方案

首先,API 10 可以通过包含 DrawerLayout 的同一 Android 支持包访问片段.这已经存在了两年多了,如果您不熟悉 Android 在过去两年中所做的事情,那么您不应该尝试弄乱诸如 DrawerLayout 之类的新事物.

其次,DrawerLayout 没有任何东西与片段相关联.引用您链接到的网页:p><块引用>

当用户在抽屉列表中选择一个项目时,系统会在给 setOnItemClickListener() 的 OnItemClickListener 上调用 onItemClick().您在 onItemClick() 方法中执行的操作取决于您实现应用程序结构的方式.

如果你仔细阅读这两个句子,你会发现它们都没有出现fragment"这个词.这是因为 DrawerLayout 没有绑定到片段.他们展示的示例代码使用了片段,但这只是示例代码.

因此,欢迎您随心所欲地更新您的 UI:

  • 使用 Android 支持包的片段反向端口执行 FragmentTransaction,或
  • 开始一项活动,或
  • 在现有活动上再次调用 setContentView(),或
  • 否则修改现有活动的 UI(例如,隐藏/显示一些小部件)

I'm trying to follow this tutorial on how to create a Navigation Drawer, but I don't want to use Fragments to show new content after the user select an item from the drawer list. What's the best aproach to get around this problem? I'm using API 10 which doesn't implements Fragments.

解决方案

First, API 10 has access to fragments via the same Android Support package that contains the DrawerLayout. This has been around for over two years, and you should not be trying to mess with new things like DrawerLayout if you are unfamiliar with what Android has had for the past two years.

Second, there is nothing with DrawerLayout that is tied to fragments. Quoting the Web page that you linked to:

When the user selects an item in the drawer's list, the system calls onItemClick() on the OnItemClickListener given to setOnItemClickListener(). What you do in the onItemClick() method depends on how you've implemented your app structure.

If you read those two sentences closely, you will see that the word "fragment" appears in neither of them. That is because DrawerLayout is not tied to fragments. The sample code they show uses fragments, but that is merely sample code.

Hence, you are welcome to update your UI however you want:

  • execute a FragmentTransaction using the Android Support package's backport of fragments, or
  • start an activity, or
  • call setContentView() again on your existing activity, or
  • otherwise modify the UI of the existing activity (e.g., hide/show some widgets)

这篇关于如何在没有片段的情况下使用导航抽屉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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