ListView控件或TableLayout? [英] ListView or TableLayout?

查看:141
本文介绍了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.

我已经学会了如何使用适配器使用的ListView 处理字符串的静态数组。我习惯在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 ,你会然后把里面的一个滚动型。但在另一方面,以达到类似的最终结果,你可以使用的ListView 以及合适适配器

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.

同样,展现项目的网格,可以垂直滚动,你可以使用 TableLayout 滚动型 。或者,类似的结果,可以从使用​​的GridView ,再通过适当的适配器提供的数据来实现的。

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.

现在,第一个关键区别在于:类像的LinearLayout TableLayout 要求您提供的子元素无论是在XML布局或交替编程方式在code。如的ListView类的GridView (和其他几个人)有很大的不同,因为它们是的子类android.widget.AdapterView 。关于适配器视图类的特别之处是,一个适配器用于将数据绑定到他们。所以,要回的垂直项目列表的例子,如果你出了一批在的LinearLayout 基于一些阵列中的数据,你将不得不子列表项编程方式创建并添加子查看 s转换的的LinearLayout 基于阵列的数据。随着的ListView 在另一方面,个别查看 s表示重新present中的子项是从供给适用适配器。因此,而不是编程方式填充的布局,所有子项(如将与的LinearLayout 的情况下,或 TableLayout 的实例),一个适配器为基础的布局,而不是调用适配器来获得孩子查看译文]当它需要他们。

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.

这最后一点是下一个关键的区别,我相信你应该明白关于适配器基于布局:哪里多,他们是更有效的呈现出大量的数据,在情况数据被滚出视。例如,的ListView 是更有效地使用用于显示项目的大型滚动列表相比,这种方式如果你只是填充一个的LinearLayout 的所有项目,并把它放在滚动型里面。这样做的原因效率是适配器视图为基础的布局通常不包含所有子查看一切都在一次。相反,通过列表中的用户滚动,现有子视图回收或转换由适配器显示下一个子元素。为了说明这一点用一个例子:你想要的100个项目滚动垂直列表。屏幕可能只有足够大,在一次显示7,但是。想象一下,你使用的LinearLayout 滚动型来显示100列表项。这意味着的LinearLayout 容器中有100个孩子浏览。这些儿童总是$在布局p $ psent和需要由系统期间滚动事件进行处理,即使只有七个可以是在视图的画面上在同一时间。这需要额外的CPU时间,相当数量的RAM,和滚动可能是缓慢的。现在,随着的ListView ,布局将只可能仅包含7或8个孩子查看秒。当用户滚动,这些子查看 s的动态转换或重新实例由适配器,通过它你绑定你数据。用户将体验到更快,更流畅的滚动操作。从编程的角度来看,它通常是更优雅通过适配器绑定数据列表。当你处理滚动位图取值列表或网格,一个安卓设备存储空间的限制也意味着使用的适配器视图是pretty的多的基础。

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.

记住,回答这个时候,我做了你要显示的项目是滚动的,可能包括垂直或表格列表中的假定位图 s,而我专注于布局,你会使用实现的布局和滚动的数据类型。布局类,如的LinearLayout TableLayout 等,不过是将要使用所有的时间,形成独特的布局重要的类积木为您的应用。如果你的整个列表是保证,以适应屏幕并不会采用滚动的,那么额外的复杂性的适配器(而不是它的真的那么复杂的)可能是毫无意义的,然后你可能只是想使用 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天全站免登陆