Difference between ScrollView and ListView
Asked Answered
R

6

43

Can any one explain the difference between Scroll View and List View? When to use which one? And which one is more efficient?

Ravelment answered 22/3, 2012 at 10:43 Comment(0)
Y
33

ScrollView is used to put different or same child views or layouts and the all can be scrolled.

ListView is used to put same child view or layout as multiple items. All these items are also scrollable.

Simply ScrollView is for both homogeneous and heterogeneous collection. ListView is for only homogeneous collection.

Yoga answered 22/3, 2012 at 10:51 Comment(3)
Based on the adapter a ListView can be just as heterogenous than a ScrollView. The difference is that ListView's contents are backed by the adapter, while ScrollView works with a single View/ViewGroup child, of course this can be built from code too.Overcharge
This answer actually misses an important point, which is that in ListView the Views are created on demand. Views in ListView are recycled, and there are never more Views in the View hierarchy than can be displayed on screen. With ScrollView all of your Views are in memory, and in the hierarchy even if they aren't displayed on screen. You get no View recycling, and it's not backed by an adapter.Baseborn
"ListView is for only homogeneous collection" : this point is not correct, and misleading the developer. ListView can also populate heterogeneous views since api level 1.Hornwort
P
29

They're completely different.

A ScrollView is simple a scrolling container you can use to scroll whatever you put inside it, which might be a list of items, or it might not.

http://developer.android.com/reference/android/widget/ScrollView.html

A ListView is very specifically designed to hold lists, where items typically look the same (or at least follow a pattern, e.g. section headings). ListView is also designed to connect to a data source of some sort, SQLite, array, content provider etc. ListView can scale to handle enormous numbers of list items.

http://developer.android.com/resources/tutorials/views/hello-listview.html

If you have data you need to show in a list, use a ListView. If you just need scrolling content, then a ScrollView is probbaly enough.

Pubilis answered 22/3, 2012 at 10:48 Comment(0)
A
3

ListView:-

In ListView You can manage layout of items in xml easily that you want to display in list.

You are required to tell the adapter ho many item you want in your display list.

You can design for both homogenous as well as heterogenous views depending on your requirement by overrifing getItemViewType() method of Adapter.

In ListView items in list are created according to screen size. i.e How many items can appear on screen are created additional views(items) are created when list is scrolled at runtime. The views that are displayed once are cached when they move out of screen and when list is scrolled back to previous state the same views are displayed but this time view are not created rather they are fetched from cache.

ScrollView :-

Cache concept is not applicable with ScrollView.

All views are created at once when they come to screen and are not cached when they move out of screen while scrolling. They are present in memory(main) that may lead to memory leak because the number of objects created are not being destroyed by garbage collector since they are being referenced untill you are on same page.

Although you can create both homogenous as well as heterogenous views. If there are more items to be displayed in your list it would be tedious to manage the layout whether you are designing in xml or creating dynamically using Java code.

It is preferable to use scrollview if you have a single page that does not contain list of items e.g registration form, reservation form but that view is larger than the screen size then put ScrollView as parent view also keep in mind that ScrollView can have only one direct child layout/view.

Adelia answered 17/12, 2016 at 10:37 Comment(0)
O
2

ScrollView simply places its contents in a scrollable container, you can edit it's contents only by adding views to it.

ListView is a class that uses an adapter which handles creating the views for your data objects, you only need to edit the data, and the layout modifications are done automatically by the adapter.

ScrollView should be used when you have a screen (ex: a form with multiple fields) that do not fit into one screen on small devices, as such scrollview offers the user the possibility to scroll down.

ListView should be used when representing sets of data.

You can read about these at http://developer.android.com/guide/index.html

Overcharge answered 22/3, 2012 at 10:49 Comment(0)
I
0

A ListView is backed by an Adapter, which contains a DataSource. This allows you to easily display data in rows.

A ScrollView allows you to put content inside of it, and if the content exceeds the size of the ScrollView, it will allow the user to scroll.

They both have their uses, but it depends on what you are trying to do.

Iceland answered 22/3, 2012 at 10:48 Comment(0)
O
0

Since an image worth a thousand words, here are perfect real life examples:

Listview is like the Kijiji app

Scrollview is like the EBay app

Also, see a scrollview like a billboard or a wall, where you can put bunch of different stuff on it.

And a listview is more like a result page: results are all of same nature, therefore they fit perfectly in a listview. Like a contacts list: they all share the same structure; phone number name address, etc....

Outfit answered 8/12, 2014 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.