在Android中使用片段而不是视图有什么好处?

时间:2022-09-06 12:35:08

When developing for Android, you can set your target (or minimum) sdk to 4 (API 1.6) and add the android compatibility package (v4) to add support for Fragments. Yesterday I did this and successfully implemented Fragments to visualize data from a custom class.

在开发Android时,您可以将目标(或最小)sdk设置为4 (API 1.6),并添加Android兼容包(v4)来添加对片段的支持。昨天我这样做了,并且成功地实现了从自定义类可视化数据的片段。

My question is this: what is the benefit for using Fragments as opposed to simply getting a View from a custom object, and still supporting API 1.5?

我的问题是:与简单地从自定义对象中获取视图并仍然支持API 1.5相比,使用片段有什么好处?

For example, say I have the class Foo.java:

例如,假设我有Foo.java类:

public class Foo extends Fragment {

    /** Title of the Foo object*/
    private String title;
    /** A description of Foo */
    private String message;

    /** Create a new Foo
     * @param title
     * @param message */
    public Foo(String title, String message) {
        this.title = title;
        this.message = message;
    }//Foo

    /** Retrieves the View to display (supports API 1.5. To use,
     * remove 'extends Fragment' from the class statement, along with
     * the method {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}) 
     * @param context Used for retrieving the inflater */
    public View getView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.foo, null);
        TextView t = (TextView) v.findViewById(R.id.title);
        t.setText(this.title);
        TextView m = (TextView) v.findViewById(R.id.message);
        m.setText(this.message);
        return v;
    }//getView 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View v = inflater.inflate(R.layout.foo, null);
        TextView t = (TextView) v.findViewById(R.id.title);
        t.setText(this.title);
        TextView m = (TextView) v.findViewById(R.id.message);
        m.setText(this.message);
        return v;
    }//onCreateView

}//Foo

Both methods are very simple to create and to work with in an Activity that, say, has a List<Foo> to display (for example, programmatically adding each to a ScrollView), so are Fragments really all that useful, or are they just an over-glorified simplification of getting a View, such as through the code above?

这两种方法都是非常简单的创建和使用一个活动,说,有一个< Foo >显示列表(例如,以编程方式添加每个滚动视图),所以片段真的有用,还是只是一个过度美化简化的视图,如通过上面的代码?

6 个解决方案

#1


163  

The main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight and simpler to implement.

使用片段的主要原因是为了支持堆栈和生命周期特性。否则,自定义视图会更轻,实现起来也更简单。

At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I ran into trouble was with the back button and life cycle. Since I was simply updating views manually...there was nothing keeping track of the history of views and their states. Therefore, the back button did not work as expected and it was difficult to recreate even the latest state during life cycle events, such as when rotating the app. To fix that, I had to wrap my custom views in fragments and use the FragmentManager so that the previous states would be saved and recreated.

一开始,我尝试使用自定义视图构建一个手机/平板应用程序。一切似乎都可以在手机和平板电脑上运行,甚至可以从一个面板切换到分割面板。我遇到麻烦的是后退按钮和生命周期。因为我只是手动更新视图……没有任何东西可以记录他们的历史和他们的国家。因此,后退按钮没有按预期工作,甚至很难重现的最新状态在生命周期事件,例如当旋转的应用。为了解决这个问题,我必须用我的自定义视图在碎片和使用FragmentManager这样以前的状态将被保存并重新创建。

I realized after answering that I posted to a similar question a year earlier: https://*.com/a/11126397/618881

我在回答了一年前的类似问题后才意识到:https://*.com/a/11126397/618881。

#2


27  

I'd say Fragments are useful in two scenarios: if you split up views on some devices/orientations and show them in two activities and show all the content in one on other devices. That would be a use case if you go on a tablet or maybe even in landscape mode on a phone: e.g. you show the list of items and the details on one screen. on a phone or in portrait mode you just show one part.

我认为片段在两种场景中是有用的:如果您在某些设备/方向上分割视图,并在两个活动中显示它们,并在其他设备上显示所有内容。如果你用的是平板电脑,或者甚至是手机的横屏模式,这就是一个用例:例如,你在一个屏幕上显示项目列表和细节。在电话或肖像模式中,你只显示一部分。

Another use case are reusable views. So if you have some views that are visible on different activities and also perform some actions you could put this behaviour into a fragment and then reuse it. Obviously you could probably do that with custom widgets too.

另一个用例是可重用视图。如果你有一些在不同活动上可见的视图并且执行了一些操作,你可以把这个行为放到一个片段中,然后重用它。显然,您也可以使用定制小部件来实现这一点。

I wouldn't see any reason for using Fragments for every View and I guess it would just be an overhead. I'm only using them in the first use case and I'd say here it is a simplification.

我不认为为每个视图使用片段有任何理由,我猜这只是一个开销。我只在第一个用例中使用它们,这里我说它是一个简化。

#3


3  

Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a back stack that's managed by the activity.

Android在Android 3.0 (API级别11)中引入了片段,主要是为了在大屏幕上支持更动态、更灵活的UI设计,比如平板电脑。因为平板电脑的屏幕比手机大得多,所以有更多的空间来组合和交换UI组件。片断允许这样的设计,而不需要您管理视图层次结构的复杂更改。通过将活动的布局划分为片段,您可以在运行时修改活动的外观,并将这些更改保存在活动管理的后堆栈中。

Here you can read more.

在这里你可以阅读更多。

#4


3  

  1. Scenario Activity Split screen - We have One Layout and one activity which handle left right screen part
  2. 场景活动分割屏幕-我们有一个布局和一个活动处理左右屏幕部分
  3. Scenario FragmentActivity we have One layout for Main screen, one for left one for right
  4. 场景碎片活动我们有一个主屏幕的布局,一个为左一个为右

Scenario one is good if you have simple application.

如果您有一个简单的应用程序,那么场景一是很好的。

Scenario two is good if you want to have Multiple Fragments and multiple FragmentActivities and you can combine each of those. Also you can make interaction between fragments.

如果您希望有多个片段和多个碎片活动,并且您可以将它们组合在一起,那么场景2是好的。您还可以在片段之间进行交互。

I have split screen Fragmentactivity i can call it with 'Intent Extras' and tell to fragmentActivity which fragment are to be loaded. Fragments are good because they are not in manifest so you could make reusable fragments and FragmentActvity.

我有分割屏幕碎片活动,我可以把它称为“意图额外的”,并告诉碎片活动的碎片将被加载。片段很好,因为它们不显化,所以您可以制作可重复使用的片段和碎片化。

But it make your project larger. But if you make large project you can save many. Because you can use same Fragments or same Fragment activity.

但它使你的项目更大。但如果你做大项目,你可以省下很多钱。因为您可以使用相同的片段或相同的片段活动。

And i thing that this fragments come little late so you must try to think in new way. Maybe just try to convert your activity to FragmentActivity. Later try to find reusable code and make Fragment from it.

我认为这些片断来得有点晚,所以你必须尝试用新的方式思考。也许你可以试着把你的活动转换成碎片活动。稍后尝试查找可重用代码并从中提取片段。

Its usefull but i dont know how right now. But i have some ideas.

它很有用,但我现在不知道该怎么做。但我有一些想法。

This is always problem. Android Team made somethink and nobody know what is good for. Because we are hardly learn like it was and here it comes some new things.

这是永远的问题。Android团队做了一些思考,没有人知道什么是好的。因为我们很难像以前那样去学习,它带来了一些新的东西。

In my opinion it is good but not for reason that google tell us.

在我看来,它是好的,但不是因为谷歌告诉我们的原因。

#5


0  

Add one case when using Fragment or Activity over CustomView:

在CustomView上使用片段或活动时添加一个案例:

When you are using CursorLoader to observe certain views, ListView or TextView and want to update their display value whenever your ContentProvider's data updates at back end(most common case you have a service which updates your local database by polling data from remote database/cloud periodically)

当您使用CursorLoader来观察某些视图、ListView或TextView,并希望在内容提供者的数据在后端更新时更新它们的显示值(最常见的情况是,您有一个服务,它通过定期从远程数据库/云轮询数据来更新本地数据库)

#6


-2  

One big thing all the above comments don't mention is that a fragment remains resident in memory even if Android kills the activity and restarts it when you do something like change the orientation of your device. This is done for performance reasons but can also lead to unexpected results if you were expecting fragments to be destroyed only to find that they are getting recreated out of nowhere.

上面所有评论都没有提到的一件重要的事情是,即使Android杀死了这个活动,并且当你改变设备的方向时重新启动它,一个片段仍然驻留在内存中。这是出于性能的原因,但如果您期望片段被破坏,却发现它们不知从哪里被重新创建,那么这也可能导致意外的结果。

#1


163  

The main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight and simpler to implement.

使用片段的主要原因是为了支持堆栈和生命周期特性。否则,自定义视图会更轻,实现起来也更简单。

At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I ran into trouble was with the back button and life cycle. Since I was simply updating views manually...there was nothing keeping track of the history of views and their states. Therefore, the back button did not work as expected and it was difficult to recreate even the latest state during life cycle events, such as when rotating the app. To fix that, I had to wrap my custom views in fragments and use the FragmentManager so that the previous states would be saved and recreated.

一开始,我尝试使用自定义视图构建一个手机/平板应用程序。一切似乎都可以在手机和平板电脑上运行,甚至可以从一个面板切换到分割面板。我遇到麻烦的是后退按钮和生命周期。因为我只是手动更新视图……没有任何东西可以记录他们的历史和他们的国家。因此,后退按钮没有按预期工作,甚至很难重现的最新状态在生命周期事件,例如当旋转的应用。为了解决这个问题,我必须用我的自定义视图在碎片和使用FragmentManager这样以前的状态将被保存并重新创建。

I realized after answering that I posted to a similar question a year earlier: https://*.com/a/11126397/618881

我在回答了一年前的类似问题后才意识到:https://*.com/a/11126397/618881。

#2


27  

I'd say Fragments are useful in two scenarios: if you split up views on some devices/orientations and show them in two activities and show all the content in one on other devices. That would be a use case if you go on a tablet or maybe even in landscape mode on a phone: e.g. you show the list of items and the details on one screen. on a phone or in portrait mode you just show one part.

我认为片段在两种场景中是有用的:如果您在某些设备/方向上分割视图,并在两个活动中显示它们,并在其他设备上显示所有内容。如果你用的是平板电脑,或者甚至是手机的横屏模式,这就是一个用例:例如,你在一个屏幕上显示项目列表和细节。在电话或肖像模式中,你只显示一部分。

Another use case are reusable views. So if you have some views that are visible on different activities and also perform some actions you could put this behaviour into a fragment and then reuse it. Obviously you could probably do that with custom widgets too.

另一个用例是可重用视图。如果你有一些在不同活动上可见的视图并且执行了一些操作,你可以把这个行为放到一个片段中,然后重用它。显然,您也可以使用定制小部件来实现这一点。

I wouldn't see any reason for using Fragments for every View and I guess it would just be an overhead. I'm only using them in the first use case and I'd say here it is a simplification.

我不认为为每个视图使用片段有任何理由,我猜这只是一个开销。我只在第一个用例中使用它们,这里我说它是一个简化。

#3


3  

Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a back stack that's managed by the activity.

Android在Android 3.0 (API级别11)中引入了片段,主要是为了在大屏幕上支持更动态、更灵活的UI设计,比如平板电脑。因为平板电脑的屏幕比手机大得多,所以有更多的空间来组合和交换UI组件。片断允许这样的设计,而不需要您管理视图层次结构的复杂更改。通过将活动的布局划分为片段,您可以在运行时修改活动的外观,并将这些更改保存在活动管理的后堆栈中。

Here you can read more.

在这里你可以阅读更多。

#4


3  

  1. Scenario Activity Split screen - We have One Layout and one activity which handle left right screen part
  2. 场景活动分割屏幕-我们有一个布局和一个活动处理左右屏幕部分
  3. Scenario FragmentActivity we have One layout for Main screen, one for left one for right
  4. 场景碎片活动我们有一个主屏幕的布局,一个为左一个为右

Scenario one is good if you have simple application.

如果您有一个简单的应用程序,那么场景一是很好的。

Scenario two is good if you want to have Multiple Fragments and multiple FragmentActivities and you can combine each of those. Also you can make interaction between fragments.

如果您希望有多个片段和多个碎片活动,并且您可以将它们组合在一起,那么场景2是好的。您还可以在片段之间进行交互。

I have split screen Fragmentactivity i can call it with 'Intent Extras' and tell to fragmentActivity which fragment are to be loaded. Fragments are good because they are not in manifest so you could make reusable fragments and FragmentActvity.

我有分割屏幕碎片活动,我可以把它称为“意图额外的”,并告诉碎片活动的碎片将被加载。片段很好,因为它们不显化,所以您可以制作可重复使用的片段和碎片化。

But it make your project larger. But if you make large project you can save many. Because you can use same Fragments or same Fragment activity.

但它使你的项目更大。但如果你做大项目,你可以省下很多钱。因为您可以使用相同的片段或相同的片段活动。

And i thing that this fragments come little late so you must try to think in new way. Maybe just try to convert your activity to FragmentActivity. Later try to find reusable code and make Fragment from it.

我认为这些片断来得有点晚,所以你必须尝试用新的方式思考。也许你可以试着把你的活动转换成碎片活动。稍后尝试查找可重用代码并从中提取片段。

Its usefull but i dont know how right now. But i have some ideas.

它很有用,但我现在不知道该怎么做。但我有一些想法。

This is always problem. Android Team made somethink and nobody know what is good for. Because we are hardly learn like it was and here it comes some new things.

这是永远的问题。Android团队做了一些思考,没有人知道什么是好的。因为我们很难像以前那样去学习,它带来了一些新的东西。

In my opinion it is good but not for reason that google tell us.

在我看来,它是好的,但不是因为谷歌告诉我们的原因。

#5


0  

Add one case when using Fragment or Activity over CustomView:

在CustomView上使用片段或活动时添加一个案例:

When you are using CursorLoader to observe certain views, ListView or TextView and want to update their display value whenever your ContentProvider's data updates at back end(most common case you have a service which updates your local database by polling data from remote database/cloud periodically)

当您使用CursorLoader来观察某些视图、ListView或TextView,并希望在内容提供者的数据在后端更新时更新它们的显示值(最常见的情况是,您有一个服务,它通过定期从远程数据库/云轮询数据来更新本地数据库)

#6


-2  

One big thing all the above comments don't mention is that a fragment remains resident in memory even if Android kills the activity and restarts it when you do something like change the orientation of your device. This is done for performance reasons but can also lead to unexpected results if you were expecting fragments to be destroyed only to find that they are getting recreated out of nowhere.

上面所有评论都没有提到的一件重要的事情是,即使Android杀死了这个活动,并且当你改变设备的方向时重新启动它,一个片段仍然驻留在内存中。这是出于性能的原因,但如果您期望片段被破坏,却发现它们不知从哪里被重新创建,那么这也可能导致意外的结果。