什么时候以及为什么应该在Android应用程序中使用片段? [英] When and why should I use fragments in Android applications?

查看:74
本文介绍了什么时候以及为什么应该在Android应用程序中使用片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要应用程序的不同部分具有自己的特殊行为和UI,而且我不知道片段如何提供帮助.在大多数情况下,我认为创建两个不同的活动(例如,为平板电脑创建1个活动,为手机创建1个活动)并在第三类中共享常见的行为和事件会更快.

I often need the different parts of my applications to have their own special behavior and UI, and I don't know how fragments can help. In most cases, I think it is quicker to create 2 different activities (e.g., 1 for tablets and 1 for handsets), and to share the common behaviors and events in a third class.

因此,请记住这一点,为什么要使用片段?

So, keeping this in mind, why should I use fragments ?

推荐答案

片段更具有UI优势.用户有时可以在同一屏幕上看到两个不同类的两个不同视图,这很方便.如果在您的创造力时刻,您决定最好用一个占一半屏幕的listView和一个占另一半的webView来显示应用程序,那么当您单击其中的一个列表项时,片段A向片段B中的webView传递了一个意图,突然之间您看到了您单击的内容,而没有应用程序切换活动-然后您可以使用一个片段.这只是我想到的一个例子.

Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen. If, in your moment of creativity, you decide it would be nice to display your application with, say, a listView that takes up half the screen and a webView that takes up the other half - so that when you click on a list item in fragment A it passes an intent to the webView in fragment B, and suddenly you see what you just clicked without the app switching activities - then you could use a fragment. That's just an example I came up with off the top of my head.

底线:片段是同时在屏幕上的两个或多个活动.

Bottom line: Fragments are two or more activities on the screen at the same time.

这篇关于什么时候以及为什么应该在Android应用程序中使用片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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