为什么使用片段,何时使用片段而不是活动? [英] Why fragments, and when to use fragments instead of activities?

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

问题描述

在 Android API 11+ 中,Google 发布了一个名为 Fragment 的新类.

In Android API 11+, Google has released a new class called Fragment.

在视频中,Google 建议尽可能(link1link2),我们应该使用片段而不是活动,但他们没有解释确切原因.

In the videos, Google suggests that whenever possible (link1, link2), we should use fragments instead of activities, but they didn't explain exactly why.

片段的目的是什么以及它们的一些可能用途(除了一些可以通过简单视图/布局轻松实现的 UI 示例)?

What's the purpose of fragments and some possible uses of them (other than some UI examples that can be easily be achieved by simple views/layouts)?

我的问题是关于片段的:

My question is about fragments:

  1. 使用片段的目的是什么?
  2. 与使用活动/视图/布局相比,使用片段的优缺点是什么?

奖励问题:

  1. 你能为片段提供一些非常有趣的用途吗?Google 在其视频中未提及的内容?
  2. 在片段和包含它们的活动之间进行通信的最佳方式是什么?
  3. 使用片段时要记住的最重要的事情是什么?您的经验有什么提示和警告吗?

推荐答案

#1 &#2 使用片段的目的是什么?什么是与使用片段相比,使用片段的优缺点活动/视图/布局?

#1 & #2 what are the purposes of using a fragment & what are the advantages and disadvantages of using fragments compared to using activities/views/layouts?

Fragment 是 Android 创建可重用用户界面的解决方案.您可以使用活动和布局(例如使用包含)来实现一些相同的事情.然而;片段被连接到 Android API,来自 HoneyComb 等.让我详细说明一下;

Fragments are Android's solution to creating reusable user interfaces. You can achieve some of the same things using activities and layouts (for example by using includes). However; fragments are wired in to the Android API, from HoneyComb, and up. Let me elaborate;

  • ActionBar.如果您希望标签在那里导航您的应用程序,您很快就会看到 ActionBar.TabListener 接口为您提供了一个 FragmentTransaction 作为 onTabSelected 的输入参数> 方法.您可能会忽略这一点,并做一些其他聪明的事情,但是您将针对 API 工作,而不是使用它.

  • The ActionBar. If you want tabs up there to navigate your app, you quickly see that ActionBar.TabListener interface gives you a FragmentTransaction as an input argument to the onTabSelected method. You could probably ignore this, and do something else and clever, but you'd be working against the API, not with it.

FragmentManager 以非常聪明的方式为您处理返回".返回并不意味着回到上一个活动,就像常规活动一样.这意味着回到之前的片段状态.

The FragmentManager handles «back» for you in a very clever way. Back does not mean back to the last activity, like for regular activities. It means back to the previous fragment state.

您可以使用很酷的 ViewPagerFragmentPagerAdapter 来创建滑动界面.FragmentPagerAdapter 代码比常规适配器更简洁,它控制各个片段的实例化.

You can use the cool ViewPager with a FragmentPagerAdapter to create swipe interfaces. The FragmentPagerAdapter code is much cleaner than a regular adapter, and it controls instantiations of the individual fragments.

如果您在尝试为手机和平板电脑创建应用程序时使用 Fragments,您的生活会轻松很多.由于这些片段与 Honeycomb+ API 紧密相连,因此您也希望在手机上使用它们来重用代码.这就是兼容性库派上用场的地方.

Your life will be a lot easier if you use Fragments when you try to create applications for both phones and tablets. Since the fragments are so tied in with the Honeycomb+ APIs, you will want to use them on phones as well to reuse code. That's where the compatibility library comes in handy.

您甚至可以而且应该将片段用于仅适用于手机的应用程序.如果您考虑便携性.我使用 ActionBarSherlock 和兼容性库来创建ICS 外观"应用程序,从 1.6 版开始,这些应用程序看起来都一样.您可以获得最新的功能,例如 ActionBar,包括标签、溢出、拆分操作栏、viewpager 等.

You even could and should use fragments for apps meant for phones only. If you have portability in mind. I use ActionBarSherlock and the compatibility libraries to create "ICS looking" apps, that look the same all the way back to version 1.6. You get the latest features like the ActionBar, with tabs, overflow, split action bar, viewpager etc.

奖金 2

在片段之间进行通信的最佳方式是意图.当您按下 Fragment 中的某些内容时,您通常会调用带有数据的 StartActivity().Intent 会传递给您启动的 Activity 的所有片段.

The best way to communicate between fragments are intents. When you press something in a Fragment you would typically call StartActivity() with data on it. The intent is passed on to all fragments of the activity you launch.

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

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