Android Fragments 中容器的用途 [英] Purpose of Container in Fragments for Android

查看:45
本文介绍了Android Fragments 中容器的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Fragment,但我无法理解为什么 Fragment 需要 Container 背后的意义.

I am learning fragments but I am failing to understand the significance behind why fragments requires a Container.

我对 Fragments 工作的理解如下:

The way I understand Fragments work is as follows :

  1. FragmentActivity setContentview 指的是一个xml文件定义片段的位置.

  1. FragmentActivity setContentview refers to a xml file which defines where fragments would be located.

FragmentActivity 创建片段的实例

FragmentActivity creates instance of the fragments

然后将片段分配给容器.

Then assigns fragment to container.

FragmentManager 然后显示它们.

FragmentManager then displays them.

实际的 Fragment 类然后膨胀一个布局,正是这个布局包含所有应用程序 UI 组件.

The actual Fragment class then inflates a layout, and it is this layout which contains all of the applications UI components.

(如果我在这里遗漏了什么,请纠正我,因为我现在只是在学习).

(please correct me if I miss something here because I am only learning at the moment).

那么为什么容器的目的是我们为什么需要,因为在我看到的所有示例中它只是一个空白的相对布局 xml 文档.

So why is the purpose of the Container why do we even need since in all the examples I have seen it is just a blank relative layout xml document.

不同的 Fragment 可以共享同一个 Container(因为它只是一个 RelativeLayout xml 文件)吗?

Can different fragments share the same Container (since its just a RelativeLayout xml file)?

所以在google提供的例子中http://developer.android.com/training/basics/片段/creating.html

So in the example provided by google http://developer.android.com/training/basics/fragments/creating.html

它们有一个 ListFragment,当通过使用 CallBack 接口选择项目时,我们最终会回到这行代码:

They have a ListFragment and when item is selected through the use of the CallBack interface we eventually get back to this line of code :

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);

我的另一个问题是:

1)为什么这行代码没有用文章片段替换ListFragment(左侧Fragment).自从它被初始化以来,我们看到:

1) Why does this line of code not replace the ListFragment (left side Fragment) with the article fragment. Since when it was initialised we see:

getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();

相反... ListFragment 保留在左侧,而右侧 Fragment 已更新.但是容器fragment_container属于firstFragment这是ListFragment.这不是更新的.

Instead ... the ListFragment remains on the left and the right Fragment is updated. But the container fragment_container belongs to firstFragment this is the ListFragment. And this is not the one that gets updated.

你明白我为什么有这个问题吗?这在教程中没有解释.

Do you see why I have the question ? This is not explained in the tutorial.

推荐答案

这里:http://marakana.com/s/post/1250/android_fragments_tutorial

这里:http://developer.android.com/guide/components/fragments.html

阅读本文,一切都会清楚:)

Read this and all will be clear:)

Fragment 是 Activity 的一部分,只能存在于 Activity 内部.所以你需要一种可以处理片段的特殊类型的活动——它是 FragmentActivity.

Fragment is a portion of Activity and can exist only inside an Activity. So you need a special type of activity that can handle fragment - it's FragmentActivity.

没有 Fragments 的 FragmentActivity 几乎就像一个普通的 Activity.但它有一个 FragmentManager 来管理(添加、删除、替换)片段.当您想将 Fragment 添加到 FragmetnActivity 时,您应该指定它应该放置的位置(因为 Fragment 不需要全屏,就像 GooglePlay - 有多个小片段).所以这就是你需要一个容器的原因.

FragmentActivity without Fragments is almost like a normal Activity. But it has a FragmentManager to manage (add,remove,replace) fragments. When you want to add a Fragment to a FragmetnActivity you should specify where it should be placed (because fragment does not need to be fullscreen, just like GooglePlay-there are multiple small fragments). So this is why you need a container.

不同的 Fragment 可以共享同一个 Container(因为它只是一个 RelativeLayout xml 文件)吗?

Can different fragments share the same Container (since its just a RelativeLayout xml file)?

是的,他们可以,您可以在同一个容器中将一个片段替换为另一个片段.

Yes they can, you can replace one fragment with another within the same container.

这篇关于Android Fragments 中容器的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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