使用 Fragment 更改部分 UI
我希望有一个带有菜单的标题,并且只能更改 UI 的其余部分(即标题和菜单永远不会重新加载,当用户在菜单中选择一个选项时会重新加载内容)。
*******************************************
* header *
*******************************************
* option1 | option2 | option3 | option4...*
*******************************************
* *
* content to be updated *
* according to the option *
* selected in the menu *
* *
* *
*******************************************
我打算使用 ViewFlipper 并添加/删除内容的布局,但我读到,对于复杂的内容,使用 Fragment 可能会更好。我见过的唯一示例是使用 TabHost 和 TabWidget,但我有自己的菜单,但不使用 TabWidget。
我可以为菜单的每个选项使用一个带有一个 Fragment 的 ViewFlipper 吗?
欢迎任何帮助/建议/指示...
谢谢
I would like to have a header with a menu and to be able to only change the remaining part of the UI (i.e. header and menu are never reloaded, content is reloaded when the user select an option in the menu).
*******************************************
* header *
*******************************************
* option1 | option2 | option3 | option4...*
*******************************************
* *
* content to be updated *
* according to the option *
* selected in the menu *
* *
* *
*******************************************
I was going to use a ViewFlipper and add/remove layout for the content, but I've read that for complex content it might be better to use a Fragment. The only example I've seen is using TabHost and TabWidget, but I have my own menu that does not use TabWidget.
Can I use a ViewFlipper with one Fragment for each of the menu's option?
Any help / suggestion / pointer welcome...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您不需要 ViewFlipper 来执行此操作。
关于 Fragments 的一点是,您可以通过简单地调用替换方法来将现有的 Fragment 替换为不同的 Fragment。这是通过使用(线性,相对,无论你喜欢哪个,但据我所知并不重要)布局并将其完全替换为片段来实现的。
假设您将布局命名为 mainContent。它是嵌入在主布局文件中的 LinearLayout,菜单位于其上方。然后,您可以使用以下方法向其添加片段(例如 OptionOne):
如果您想将其替换为 OptionTwo 的内容,则可以简单地使用以下方法替换它:
注意:我使用了 Fragments 的兼容版本为此,您的代码可能会有所不同。
此外,为了能够使用此方法,您还需要将要使用的所有当前活动也更改为
Fragments
。You actually don't need a ViewFlipper to do this.
The thing about Fragments is that you can replace an existing Fragment with a different one by simply calling a method to replace it. This works by using a (Linear, Relative, whichever you prefer but it doesn't matter as far as I have noticed) layout and replacing it completely with a fragment.
Say you named the layout mainContent. It's a LinearLayout embedded in a main layout file with your menu above it. You can then add a fragment (for example, OptionOne) to it by using the following method:
If you want to replace it by the content of OptionTwo, you can simply replace it by using this:
Note: I used a compatibility version of Fragments for this so your code might be different.
Also, to be able to use this method, you will need to change all of your current activities that you want to use like this to
Fragments
as well.