为什么要使用片段,以及何时使用片段代替活动? [英] Why fragments, and when to use fragments instead of activities?

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

问题描述

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

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

在视频中,Google建议在可能的情况下( link1 link2 ),我们应该使用片段而不是活动,但它们并不能确切说明原因.

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?

片段是Android创建可重用用户界面的解决方案.您可以使用活动和布局(例如,使用包含)来实现某些相同的目的.然而;片段通过HoneyComb以及更高版本连接到Android API.让我详细说明;

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

片段之间进行交流的最佳方式是意图.当您按片段中的某个内容时,通常会调用StartActivity()并带有数据.目的会传递给您启动的活动的所有片段.

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天全站免登陆