Search code examples
javaandroidviewgroup

Android adding multiple views to viewgroup


I have a simple question. I have an arrayList of views I want to add to a ViewGroup. I currently am iterating through them with a for loop and adding them individually.

ViewGroup commentList = (ViewGroup) this.findViewById(R.id.comment_list);
for (View commment: comments) {
    commentList.addView(commment);
}

Can I do this all at once with a single call? Like an addAll() method? I feel like this would be more efficient, especially if I have a lot of views I'm adding....


Solution

  • Iterating through the views if there are a lot is fairly efficient. If this ViewGroup is going to always have a lot of views you may want to consider using a view type that uses an adapter and recycling / reuse otherwise you'll have a lot of views that are in memory yet are off screen.