[英]How to use Fragments in Android properly
I asked a question a while back: 我回答了一个问题:
Replacing Fragments isn't working/Am I executing this the proper way? 替换碎片不起作用/我是否以正确的方式执行此操作?
Because I was having a really tough time wrapping my head around the entire concept. 因为我很难将整个概念包裹起来。 Past Google I/O videos have also helped, but I still didn't really have a reason to use fragments. 过去的Google I / O视频也有所帮助,但我仍然没有理由使用片段。 I have encountered my problem again, and with more searching and Google I/O 2013 videos, I've hit another fork in the road. 我再次遇到了我的问题,通过更多的搜索和Google I / O 2013视频,我又遇到了另一个问题。 After starting a bounty to get a more expert opinion on this subject, I was guided to use FrameLayouts, yet in this I/O 13 video at 19:05 into the video it shows <fragment>
. 在开始赏金以获得关于此主题的更专家意见之后,我被引导使用FrameLayouts,但在此I / O 13视频中19:05进入视频显示<fragment>
。 Can someone clear up any discrepencies or misconceptions I have? 有人可以澄清我的任何不满或误解吗?
Do I use <fragment>
or <frameLayout>
? 我使用<fragment>
还是<frameLayout>
? Bonus points if you look at my original question to know where I'm coming from. 如果你看看我原来的问题,知道我来自哪里,可以获得奖励积分。
There are 2 seperate questions I see in here; 我在这里看到两个单独的问题; (1) what is the <fragment>
element? (1)什么是<fragment>
元素? (2) am I (meaning you) doing what I tried to do in my previous question in the best way possible by using a FrameLayout
?. (2)我(意思是你)通过使用FrameLayout
以最好的方式做我在上一个问题中尝试做的事情吗?
I will try to answer both. 我会尽力回答这两个问题。
The <fragment>
XML tag is just another way to implement a fragment. <fragment>
XML标签只是实现片段的另一种方式。 As a refresher, a Fragment
is like a part of an activity. 作为复习, Fragment
就像是活动的一部分。 It is a stand alone class, however, referring back to the fragment documentation , because a Fragment
is intended to be modular, all fragments must be housed within a seperate FragmentActivity
. 然而,它是一个独立的类,回顾片段文档 ,因为Fragment
是模块化的,所有片段必须存放在单独的FragmentActivity
。 Think of this as FragmentActivity
as a container. 将此视为FragmentActivity
作为容器。 By loading your Fragment
into the FragmentActivity
the FragmentActivity
provides other valuable functions that the Fragment
class does not have access to on its own. 通过将Fragment
加载到FragmentActivity
, FragmentActivity
提供了Fragment
类本身无法访问的其他有价值的函数。 It also offers you the chance to swap out fragments dynamicly, allowing for a more flexable user experience. 它还为您提供动态交换片段的机会,从而提供更灵活的用户体验。
How you load your Fragment
into its containing FragmentActivity
depends on what you are trying to acomplish. 如何将Fragment
加载到其包含的FragmentActivity
取决于您要尝试的内容。 One way to do this is to use the <fragment>
tag. 一种方法是使用<fragment>
标签。 With this approach, rather than declaring the Fragment
in the activity java code and then loading it with a FragmentTransaction
as you did in your earlier question, the Fragment
class is loaded via the XML layout for the containing FragmentActivity
. 使用这种方法,不是像在前面的问题中那样在活动java代码中声明Fragment
然后使用FragmentTransaction
加载它,而是通过包含FragmentActivity
的XML布局加载Fragment
类。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
You can see the fragment declared here in the example taken from the android docs. 您可以在Android文档中看到示例中声明的片段。 The fragment is then loaded by the FragmentActivty
when setContentView()
is called onCreate
. 然后,当onCreate
调用setContentView()
时, FragmentActivty
由FragmentActivty
加载。
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
}
}
The other way to display fragments is as you have done it in your prior example. 显示片段的另一种方式就像您在前面的示例中所做的那样。 Using a FragmentManager
you can create a new instance of your Fragment
class and load it programatically within the java code. 使用FragmentManager
您可以创建Fragment
类的新实例,并在java代码中以编程方式加载它。
So which technique makes sense when? 那么哪种技术有意义呢? As the example from the android docs demonstraits, the XML fragment method makes sense when you are using stationary fragments, ie you are not dynamically swapping fragments in and out. 作为android docs demonstraits的例子,当你使用固定片段时,XML片段方法是有意义的,即你不是动态地交换片段。 This is because you cannot edit the XML layout you create on the fly the same way you can the java code. 这是因为您无法像在java代码中那样编辑动态创建的XML布局。 Also, from what I can tell, using the XML approach treats the fragments more like layout elements alowing for different control. 另外,从我所知道的,使用XML方法将片段更像是用于不同控制的布局元素。 In the android example, they use the XML approach to display 2 fragmens side by side on a tablet. 在android示例中,他们使用XML方法在平板电脑上并排显示2个fragmens。 By controling the fragments more like layout elements, eg having the ability to adjust layout_weight
you can achieve a better result for that purpose. 通过控制片段更像布局元素,例如具有调整layout_weight
的能力,您可以为此目的获得更好的结果。
However, if you are designing a highly dynamic UX, say with a ViewPager
or some other feature where fragments will be rotated regularly, it makes more sense to use seperate Fragment
classes and replace those Fragments
as they become necessary. 但是,如果您正在设计一个高度动态的用户体验,例如使用ViewPager
或其他一些片段将定期轮换的功能,那么使用单独的Fragment
类并在必要时替换这些Fragments
更有意义。
Based on your prior question and the need to swap fragments dynamicly I think you made the right choice for that implementation. 基于您之前的问题以及动态交换片段的需要,我认为您为该实现做出了正确的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.