Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

android - ListView or TableLayout?

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...