Android中的“片段中的容器”的目的 [英] Purpose of Container in Fragments for Android

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

问题描述

我正在学习片段,但是我无法理解为什么片段需要容器的原因。

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

我理解片段工作的方式如下:

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).

所以为什么还要使用Container的目的,因为在所有示例中我都看到它只是一个空白的相对布局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.

不同的片段可以共享同一个容器吗(因为它只是一个RelativeLayout xml文件)?

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

因此在google http提供的示例中: //developer.android.com/training/basics/fragments/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

在这里:< a href = http://developer.android.com/guide/components/fragments.html rel = nofollow> http://developer.android.com/guide/components/fragments.html

阅读此书,一切都会清楚:)

Read this and all will be clear:)

片段是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.

不带片段的FragmentActivity几乎就像一个正常的Activity。但是它有一个FragmentManager来管理(添加,删除,替换)片段。当您想将片段添加到FragmetnActivity时,应指定其放置位置(因为片段不需要全屏显示,就像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.


不同的片段可以共享同一容器吗(因为它只是一个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中的“片段中的容器”的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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