不要碎片造成脂肪的活动? [英] Do fragments cause fat activities?

查看:92
本文介绍了不要碎片造成脂肪的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我得到了建立一个应用程序,它有点像一本书的任务。高层次的设计基本上是一对夫妇的列表视图显示章节,然后向下钻取到的主题列表,然后到自己的页面。有一些这显示书签或文本搜索结果,并有动画了相当数量的,滑动一个片段关闭其他列表类型的意见和另一个在同一时间。这是我第一次使用的片段,虽然他们首先似乎是一块UI功能的一个很好的封装,我不禁想,如果他们使用,因为在我的情况,导致了'胖'活动课,有管理几个不同的片段,并具有实现他们发布了几个接口。

作为一个例子,说明一个活动实现的接口:

 公共类NodeListActivity扩展活动器具
        NodeListFragment.OnItemSelectedListener,
        SearchListFragment.OnItemSelectedListener,
        NodeFragment.OnLinkSelectedListener,
        OnCloseSelectedListener,
        OnActionBarItemSelectedListener,
        OnBookmarkSelectedListener
 

在旧版本的Andr​​oid,我早就会为每个主屏幕/功能的独立活动。 这是一个code气味或做的几个片段的组合和动画导致脂肪活动?

解决方案
  

做的几个片段的组合和动画导致脂肪活动?

是的。然而,正变得越来越普遍,因此不太code臭。

什么是有点臭的是,loooooooooooooooooooong接口列表。 : - )

有关的东西,复杂的,你可能会发现,使用事件总线(Sq​​uare的奥托,greenrobot的EventBus,甚至 LocalBroadcastManager )是一个更清洁的方式。而不是片段用一个接口来讲述事件的托管行为(这是我的猜测,所有的接口的源),该片段能事件发布到总线上,它托管的活动可以订阅。这确实增加了运行时开销微幅下挫,但增加了避免所有接口的利益。它也可以扩展到其他组件,诸如服务发布一个事件有事时,所述UI层,如果存在的话,需要知道关于背景

Recently I was given the task of building an app which is rather like a book. The high-level design was basically a couple of list views showing chapters, then drill-down into a list of topics then onto a page itself. There were a number of other list-type views which displayed bookmarks or text search results and there was a fair amount of animation, sliding one fragment off and another on at the same time. This was my first use of fragments and although they first appear to be a good encapsulation of a piece of UI functionality, I can't help wondering if their use, as in my case, leads to 'fat' Activity classes, having to manage several different fragments and having to implement several interfaces which they publish.

As an example showing the interfaces implemented by one Activity:

public class NodeListActivity extends Activity implements 
        NodeListFragment.OnItemSelectedListener,
        SearchListFragment.OnItemSelectedListener,
        NodeFragment.OnLinkSelectedListener,
        OnCloseSelectedListener,
        OnActionBarItemSelectedListener,
        OnBookmarkSelectedListener

On older versions of Android, I would have created a separate Activity for each main screen/function. Is this a code smell or does the combination of several fragments and animations lead to fat Activities?

解决方案

does the combination of several fragments and animations lead to fat Activities?

Yes. However, that is becoming more common, and therefore less code smelly.

What is somewhat smelly is that loooooooooooooooooooong list of interfaces. :-)

For something that complex, you might find that using an event bus (Square's Otto, greenrobot's EventBus, or even LocalBroadcastManager) is a cleaner approach. Rather than having fragments use an interface to tell their hosting activity about an event (which is my guess as to the source of all your interfaces), the fragment can post an event to the bus, which the hosting activity can subscribe to. This does add a smidge of runtime overhead, but adds the benefit of avoiding all the interfaces. It can also be extended to other components, such as a service posting an event when something happens in the background that the UI layer, if it exists, needs to know about.

这篇关于不要碎片造成脂肪的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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