且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有片段的情况下使用导航抽屉?

更新时间:2022-10-19 13:21:53

首先,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)