ListView 还是 TableLayout? [英] ListView or TableLayout?

查看:18
本文介绍了ListView 还是 TableLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在真的很困惑要学习哪个.我是一名 iPhone 应用开发者,现在正在学习 Android 开发.

I am really confused now as to which one to learn. I am an iPhone app developer and now learning Android development.

我已经学习了如何使用 AdapterListView 与字符串的静态数组一起使用.我习惯在 iPhone 中使用自定义单元格,主要用于在 TableView 中显示动态内容,如图像和文本.

I have learnt how to use a ListView with a static array of strings using an Adapter. I am used to using custom cells in iPhone, mostly for showing dynamic content like images and text in TableViews.

那么在 Android 中这样做的方法是什么?TableLayout 还是 ListView?

So which is the way to go for doing that in Android? TableLayout or ListView?

推荐答案

正如其他人在评论中已经说过的那样,您需要先明确定义您想要做什么,然后再具体决定使用哪种布局类型.但是,我当然可以理解尝试决定使用哪种类型的布局类时出现的困惑,因为通常有几种可供选择来实现相同的目标.例如,要创建一个垂直滚动的项目列表,您可能首先选择一个垂直的 LinearLayout,然后将其放置在 ScrollView 中.但另一方面,为了获得类似的最终结果,您可以将 ListView 与合适的 Adapter 一起使用.

As others have already said in comments, you need to clearly define what you want to do first before making a concrete decision on which type of layout to use. However, I can certainly understand the confusion that arises from trying to decide over which type of layout class to use, because there are often several to choose from to achieve the same objective. For example, to create a vertically-scrolling list of items, you might at first choose a vertical LinearLayout, which you'd then place inside a ScrollView. But on the other hand, to achieve a similar end result, you could use a ListView together with a suitable Adapter.

同样,要显示可以垂直滚动的项目网格,您可以在 ScrollView 中使用 TableLayout.或者,使用 GridView 也可以获得类似的结果,同样通过合适的 Adapter 提供数据.

Similarly, to show a grid of items that can scroll vertically, you might use a TableLayout inside a ScrollView. Or, a similar result could be achieved from using a GridView, again by supplying data through a suitable Adapter.

现在,第一个关键区别在于:LinearLayoutTableLayout 之类的类要求您以 XML 布局或以编程方式在代码中提供子元素.ListViewGridView(以及其他几个)类非常不同,因为它们是 android.widget.AdapterView 的子类.AdapterView 类的特殊之处在于使用 Adapter 将数据绑定到它们.因此,回到垂直项目列表的示例,如果您基于某些数组数据在 LinearLayout 内显示一组子列表项,则必须以编程方式创建和添加子 View 根据数组数据进入 LinearLayout .另一方面,对于 ListView,代表子项的各个 View 由合适的 Adapter 提供.因此,不是您以编程方式用所有子项填充布局(例如 LinearLayoutTableLayout 的情况),Adapter-based 布局改为调用 Adapter 以在需要时获取子 View.

Now, the first key difference is this: Classes like LinearLayout and TableLayout require you to supply the child elements either in XML layouts or alternatively programmatically in code. Classes like ListView and GridView (and several others) are very different because they are subclasses of android.widget.AdapterView. The special thing about AdapterView classes is that an Adapter is used to bind data to them. So, going back to the example of a vertical list of items, if you were showing a group of child list items inside a LinearLayout based upon some array data, you would have to programmatically create and add child Views into that LinearLayout based upon the array data. With ListView on the other hand, the individual Views that represent the child items are supplied from a suitable Adapter. So, rather than you programmatically filling the layout with all child items (as would be the case with LinearLayout or TableLayout for instance), an Adapter-based layout instead calls the Adapter to obtain the child Views as and when it needs them.

最后一点是下一个关键区别,我相信您应该了解基于 Adapter 的布局:它们在显示大量数据时效率更高,在大部分数据被滚出的情况下看法.例如,使用 ListView 来显示大型滚动项目列表比简单地用所有项目填充 LinearLayout 并将其放入其中更有效一个 ScrollView.这种效率的原因是基于 AdapterView 的布局通常不会同时包含所有子 View.相反,当用户滚动列表时,现有的子视图被 Adapter回收"或转换"以显示下一个子元素.举个例子来说明这一点:您需要一个包含 100 个项目的垂直滚动列表.然而,屏幕可能只能一次显示 7 个.想象一下,您在 ScrollView 中使用 LinearLayout 来显示 100 个列表项.这意味着 LinearLayout 容器有 100 个子 Views.这些孩子总是出现在布局中,需要在滚动事件期间由系统处理,即使一次只能在屏幕上看到七个.这需要额外的 CPU 时间、大量的 RAM,并且滚动可能会很缓慢.现在,使用 ListView,布局可能只包含 7 或 8 个子 View.当用户滚动时,那些子 View 会由您绑定数据的 Adapter 动态转换或重新实例化.用户将体验到更快、更流畅的滚动操作.从编程的角度来看,通过 Adapter 绑定数据列表通常要优雅得多.当您处理 Bitmap 的滚动列表或网格时,Android 设备的内存限制也意味着 AdapterView 的使用是非常重要.

That last point is the next key difference I believe you should understand about Adapter based layouts: They are much more efficient at showing large amounts of data, in situations where much of the data is scrolled out of view. For example, a ListView is much more efficient to use for displaying a large scrolling list of items than would be if you simply populated a LinearLayout with all the items and put it inside a ScrollView. The reason for this efficiency is that AdapterView-based layouts do not generally contain all child Views all at once. Instead, as the user scrolls through the list, existing child views are "recycled" or "converted" by the Adapter to show the next child elements. To illustrate this with an example: You want a scrolling vertical list of 100 items. The screen may only be large enough to display 7 at once, however. Imagine you use a LinearLayout inside a ScrollView to show 100 list items. That means the LinearLayout container has 100 child Views. Those children are always present in the layout and need to be processed by the system during scroll events, even if only seven can be in view on the screen at one time. This takes extra CPU time, a considerable amount of RAM, and scrolling may be sluggish. Now, with a ListView, the layout will only probably contain just 7 or 8 child Views. As the user scrolls, those child Views are dynamically converted or re-instantiated by the Adapter through which you bind your data. The user will experience a faster, smoother scrolling operation. From a programming point of view, it is usually far more elegant to bind lists of data through an Adapter. When you're dealing with scrolling lists or grids of Bitmaps, the memory constraints of an Android device also mean the use of an AdapterView is pretty much essential.

请记住,在回答这个问题时,我假设您正在尝试显示可滚动的垂直或表格项目列表,可能包括 Bitmap,并且我'm 专注于您用于实现该数据的布局和滚动的布局类型.像 LinearLayoutTableLayout 等布局类都是很重要的类,您将一直使用它们来为您的应用程序形成单独的布局构建块.如果你的整个列表保证适合屏幕并且不能滚动,那么使用 Adapter 的额外复杂性(不是它真的那么复杂)可能毫无意义,然后你可能只想使用 TableLayout 或其他什么.

Bear in mind that when answering this, I've made the assumption that you're trying to show a vertical or tabular list of items that is scrollable, perhaps including Bitmaps, and I'm concentrating on the type of layout that you'd use for achieving the layout and scrolling of that data. Layout classes like LinearLayout, TableLayout, etc. however are important classes that you will use all the time to form individual layout building blocks for your applications. If your entire list is guaranteed to fit into the screen and won't be scrollable, then the additional complexity of using an Adapter (not that it is really that complicated) may be pointless and you might then just want to use a TableLayout or whatever.

这篇关于ListView 还是 TableLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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