什么是创造一个片段VS扩展视图或的ViewGroup的优势是什么? [英] What are the advantages of creating a Fragment vs Extending a View or ViewGroup?

查看:134
本文介绍了什么是创造一个片段VS扩展视图或的ViewGroup的优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我们转换一个应用程序从基于一个多活动,一个与多个片段单个活动。 ,成为用于盛装碎片自己的碎片,所以我们用孩子的片段经理主办的片段,在片段的活动(这些子片段我要补充的,体积小,可以有4个或5这些在屏幕上一次)。

这已经引起了一些问题,即必须创建和跟踪唯一ID的片段持有人。 (与Backstack以及如果有的话是任何类型AdapterViews的处理时引起的头痛)。

我们正在考虑只是重写这些组件来扩展某种ViewGroup中,可能的FrameLayout或LinearLayout中的。我们已经做到这一点无论如何在某些情况下,但我不知道是否有什么缺点做呀?(我必须承认,我真的没有看到有关片段大不了......任何你可以用片段做,你可以通过创建一个自定义视图做的。这是错的?)。

解决方案

这是一个有点相反的答案,解释片段使用。一般来说,你可以做活动最多的事,但作为SDK支持的片段,很多事情都会变得更麻烦(ViewPager是一个例子,片段的使用是很大的)。

(片段)的优点: code封装,用户界面​​的可重复使用的块。

更多code(如FragmentManager,FragmentTransaction)

:(片段)

的缺点

您原来使用的活动还是不错的,这不是在那里我会切换到碎片的情况。

比方说,我设计了两个活动,一个手机应用程序:ContactList和ContactDetails。到目前为止好,没有理由使用碎片呢。如果我想支持更大的设备,它会理所当然我要显示两个屏幕并排放置。这就是碎片派上用场。在究竟如何构建活动/片段来看,有一些很好的建议是:

https://developer.android.com/guide/practices/tablets-and-handsets.html#Fragments

下面是重要的位:

  

多个片段,一个活动:

     

使用一个活动不管   装置的大小,但在运行时决定是否要结合片段在   布局(以创建多窗格设计)或交换片段(创建   单面板设计)。

或者

  

多个片段,多重活动:

     

在平板电脑,将多个片段在一个活动的;在手持设备上,   使用单独的活动举办的每个片段。例如,当   片剂设计使用两个片段中的活性,使用相同的活性   对于手机,而是提供一个替代的布局只包括   第一个片段。在手机上运行,​​你需要切换时   片段(当用户选择一个项目,如),开始另一   承载第二个片段的活性。

  

您所选择的方法   取决于你的设计和个性化preferences。第一个选项(一个   活动;交换片段),您需要确定屏幕   大小在运行时动态添加的每个片段   适当的,而不是申报的活动的XML片段   布局,因为如果它是从活动中无法删除的片段   在XML布局已宣告。当使用第一种技术,则   可能还需要在每次更新操作栏上的碎片   改变,这取决于可用的动作或导航模式   每个片段。在某些情况下,这些因素可能不会影响你的   设计,因此使用一个活动和交换的碎片可能会工作得很好   (特别是如果你的平板电脑的设计要求添加片段   动态反正)。其他时候,然而,动态交换   为您的手机设计的片段可以让你的code比较复杂,   因为你必须管理所有的片段组合中   活动的code(而不是使用替代布局资源   定义片段组合)和管理片段回栈   自己(而不是允许正常活动栈处理   背导航)。

We recently converted an app from a multiple activity based one, to one with a single activity with multiple fragments. The activities that became Fragments used to contain fragments themselves, so we use child fragment managers to host the Fragments in the Fragments (these child fragments I should add, are small and there can be 4 or 5 these on the screen at one time).

This has caused a few issues, namely having to create, and keep track of Unique IDs for the Fragment holders. (Which cause headaches when dealing with the Backstack as well as if any are in any sort of AdapterViews).

We're thinking of just rewriting these components to extend some sort of ViewGroup, likely FrameLayout or LinearLayout. We already do that anyway in some cases, but I was wondering if there are any disadvantages to doing it that way? (I must admit, I don't really see the big deal about Fragments... anything you can do with Fragments, you can do by creating a Custom View. Is this wrong?).

解决方案

This is a bit of a reverse answer, explaining fragment use. In general you can do most things with Activities, but as the SDK supports fragments, many things will become more cumbersome (ViewPager is an example where fragment use is great).

The advantages (of fragments): code encapsulation, reusable chunks of UI.

The disadvantages (of fragments): more code (e.g. FragmentManager, FragmentTransaction).

Your original use of activities was good, it's not a case where I would have switched to fragments.

Let's say I designed a mobile phone app with two activities: ContactList and ContactDetails. So far so good, no reason to use fragments yet. If I wanted to support a larger device, it would behoove me to display both screens side by side. This is where fragments come in handy. In terms of exactly how to structure your activities/fragments, there's some good advice here:

https://developer.android.com/guide/practices/tablets-and-handsets.html#Fragments

Here are the important bits:

Multiple fragments, one activity:

Use one activity regardless of the device size, but decide at runtime whether to combine fragments in the layout (to create a multiple-pane design) or swap fragments (to create a single-pane design).

Or...

Multiple fragments, multiple activities:

On a tablet, place multiple fragments in one activity; on a handset, use separate activities to host each fragment. For example, when the tablet design uses two fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just the first fragment. When running on a handset and you need to switch fragments (such as when the user selects an item), start another activity that hosts the second fragment.

.

The approach you choose depends on your design and personal preferences. The first option (one activity; swapping fragments) requires that you determine the screen size at runtime and dynamically add each fragment as appropriate—rather than declare the fragments in your activity's XML layout—because you cannot remove a fragment from an activity if it's been declared in the XML layout. When using the first technique, you might also need to update the action bar each time the fragments change, depending on what actions or navigation modes are available for each fragment. In some cases, these factors might not affect your design, so using one activity and swapping fragments might work well (especially if your tablet design requires that you add fragments dynamically anyway). Other times, however, dynamically swapping fragments for your handset design can make your code more complicated, because you must manage all the fragment combinations in the activity's code (rather than use alternative layout resources to define fragment combinations) and manage the back stack of fragments yourself (rather than allow the normal activity stack to handle back-navigation).

这篇关于什么是创造一个片段VS扩展视图或的ViewGroup的优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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