本文介绍了为什么要使用片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Fragment 比使用在不同布局中重复使用的自定义 View 有什么优势?

What is the advantage to using Fragments over using custom Views that are reused in different layouts?

原始博客文章中介绍片段, Dianne Hackborn 说

In the original blog post introducing fragments, Dianne Hackborn says that

[Fragments] 使开发人员可以更轻松地编写可扩展的应用程序跨越各种屏幕尺寸,超越现有设施在平台上可用.

然后她继续在为应用制作平板电脑布局的上下文中解释 Fragments,该应用结合了来自同一应用的手机版本的两个 Activity 的 UI.

and she goes on to explain Fragments in the context of making a tablet layout for an app that combines the UI of two activities from the phone version of the same app.

但似乎使用自定义视图可以实现相同的重用.Fragment 和 Views 之间的主要区别似乎是它们具有不同的生命周期......

But it seems that the same reuse could be achieved using custom Views. The main different between Fragments and Views seems to be that they have differing lifecycles...

Fragment 生命周期是:

onAttach()onCreate()onCreateView()onActivityCreated()onStart()onResume()onPause()onStop()onDestroyView()onDestroy()onDetatch().

onAttach(), onCreate(), onCreateView(), onActivityCreated(), onStart(), onResume(), onPause(), onStop(), onDestroyView(), onDestroy(), onDetatch().

View 生命周期是:

ctoronFinishInflate()onAttachedToWindow()onMeasure()onLayout(), onDetatchedFromWindow()

我想听听具有编写大型应用程序经验的开发人员的意见,了解他们在使用 Fragment 与自定义视图将 UI 分成可重复使用的部分时所看到的好处(如果有的话).

I'd like to hear from developers with experience writing large apps about what benefits (if any) they've seen in using Fragments vs custom Views to divide up the UI into reusable pieces.

推荐答案

主要原因是 fragment 比自定义视图更容易重用.

有时您无法仅依靠视图来创建完全封装的 UI 组件.这是因为有些东西你想放入你的视图中,但不能,因为只有一个 Activity 可以处理它们,从而迫使 Activity 和视图之间紧密耦合.

Sometimes you can't create a fully encapsulated UI component relying on views alone. This is because there are things you would want to put into your view but can't because only an Activity can handle them, thus forcing tight coupling between an Activity and a View.

这是一个这样的例子.假设您想要创建一个可重用的 UI 组件,其中包括很多事情,想要捕捉照片并用它做一些事情.传统上,您会触发一个意图,启动相机并返回捕获的图像.

Here is one such example. Lets say you want to create a reusable UI component that, among many things, want to capture a photo and do something with it. Traditionally you would fire an intent that starts the camera and returns with the captured image.

请注意,您的自定义 UI 组件无法完全封装此功能,因为它将不得不依赖托管 Activity 的 startActivityForResult,因为视图不接受 Activity 结果(它们可以通过上下文间接触发意图).

Notice that your custom UI component can't fully encapsulate this functionality because it will have to rely on hosting Activity's startActivityForResult because views don't accept activity results (they can indirectly fire an intent through context).

现在,如果您想在不同的 Activity 中重用自定义 UI 组件,您需要重复 Activity.startActivityForResult 的代码.

Now if you wanted to reuse your custom UI component in different activities you would be repeating the code for Activity.startActivityForResult.

另一方面,Fragment 干净利落地解决了这个问题.

Fragment on the other hand cleanly solve this problem.

类似地,您的片段可以为您的选项菜单提供项目,这在传统上只有 Activity 才能做到.同样,如果您的自定义视图的状态决定了菜单中的内容,这可能很重要.

Similarly your fragment can contribute items to your options menu, something traditionally only an Activity could do. Again this could be important if the state of your custom view dictates what goes in the menu.

这篇关于为什么要使用片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:03