1

Suppose I have a list of items like this:

List<User> mPersonList=getAllUser();

Then I want to display them in the activity, I have two ideas:

1 Use the ListView

I will create a Custom adapter extending from ArrayAdapter to bind the data to the view. This this the general idea.

But sometimes, the parent of a ListView may(must) be a ScrollView, so someone suggest me use the Linearlayout.

2 Use something like the LinearLayout.

Create one LinearLayout for each item, and add them to the parent view.

For example:

ViewGroup parent=(ViewGroup)findViewById(R.id.xx):
for(Person p : mPersonList){
  LinearLayout ll=new LinearLayout(...) //created by constructor
  // or ll=mlayerInflate.inflate(R.layout.xxx);
  TextView tv=new TextView(..)
  tv.setText(p.name);
  ll.addView(tv);
  parent.addView(ll);
}

I wonder if which is better when performance considered?

Community
  • 1
  • 1
hguser
  • 35,079
  • 54
  • 159
  • 293

1 Answers1

0

ListView will be much better for performance than implementing the same in a LinearLayout.

The ListView will only inflate as many views as will fit on the screen at any one time. Inflating views is much more expensive than changing the values of a view, so this is much better.

EDIT:

However, you are right that if you are putting the ListView inside a ScrollView it will not work correctly. in that case you should probably use a LinearLayout instead, or perhaps re-work your layout so that you don't use the ScrollView parent (such as using ListView header views etc)

panini
  • 2,026
  • 19
  • 21
  • But how about if I have to put a `ListView` inside a `ScrollView`? – hguser Nov 20 '13 at 00:44
  • Definitely don't put a ListView inside a scrollview. It will collapse the ScrollView. I fought this one quite a bit early on. I generally add them to a LinearLayout or RelativeLayout. – Mark Freeman Nov 20 '13 at 01:01