Android的 - 需要片段VS活动和观点的一些澄清 [英] android - need some clarifications of fragments vs activities and views

查看:126
本文介绍了Android的 - 需要片段VS活动和观点的一些澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的API 11+,谷歌已经发布了一个新的类名为片段

在视频,谷歌认为,只要有可能( LINK1 ,的link2 ),我们应该用片段,而不是活动,但是他们并没有解释究竟为什么。

什么片段的目的,而他们中的一些可能的用途

(比,可以很容易地通过简单的次/布局实现一些UI例子之外)

我的问题是有关片段:

  1. 什么是使用一个片段的目的是什么?
  2. 有哪些优点和使用片段相比,使用活动/视图/布局的缺点是什么?

奖金问题:

<醇开始=3>
  • 你能给一些真正有趣的用途的片段?事情,谷歌在他们的影片没有提及?
  • 什么是碎片,包含它们的活动之间沟通的最佳方式是什么?
  • 什么是当您使用碎片要记住最重要的事情?从您遇到任何提示和警告?
  • 解决方案
      

    #1和; #2什么是使用一个片段和放大器的目的;什么是   优点和使用片段的缺点与使用   活动/视图/布局?

    片段Android的解决方案,以创建可重用的用户界面。你可以实现一些使用活动和布局(例如,通过使用包括)同样的事情。然而;片段连接到Android的API,从蜂巢,和了起来。让我解释;

    • 动作条。如果你想卡在那里浏览您的应用程序,你很快就会看到 ActionBar.TabListener 接口,为您提供了 FragmentTransaction 作为输入参数的 onTabSelected 方法。你可能会忽略这一点,和做其他的事情和聪明,但你会工作对API,而不是用它。

    • FragmentManager 处理«返回»你在一个非常聪明的方式。返回并不意味着回到过去的活动,如定期活动。这意味着回previous片段状态。

    • 您可以使用酷<​​code> ViewPager 与 FragmentPagerAdapter 以创建刷卡接口。该 FragmentPagerAdapter code是比普通的适配器更清洁,它控制个别片段的实例。

    • 如果您使用的碎片,当您尝试创建为手机和平板电脑的应用程序你的生活会轻松很多。由于片段是如此绑在与蜂窝+的API,你将要使用他们的手机以及重用code。这就是兼容性库就派上用场了。

    • 您甚至可以和应该使用片段的意思只有手机的应用程序。如果您有便携性的初衷。我用 ActionBarSherlock 和兼容性库打造ICS看的应用程序,看起来都一样的方式回到1.6版。你得到最新功能的动作条,使用标签,溢出,分裂行动起​​来吧,viewpager等。

      

    奖金2

    最好的办法之间的片段意图沟通。当你在一个片段,你通常会叫 StartActivity()与它的数据preSS的东西。这样做的目的是传递给你启动活动的所有片段。

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

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

    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. What are the purposes of using a fragment?
    2. What are the advantages and disadvantages of using fragments compared to using activities/views/layouts?

    Bonus questions:

    1. Can you give some really interesting uses for fragments? Things that Google didn't mention in their videos?
    2. What's the best way to communicate between fragments and the activities that contain them?
    3. What are the most important things to remember when you use fragments? Any tips and warnings from your experience?

    解决方案

    #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?

    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;

    • 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.

    • 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.

    • 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.

    • 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.

    • 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.

    Bonus 2

    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.

    这篇关于Android的 - 需要片段VS活动和观点的一些澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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