Vishal Vyas
Vishal Vyas

Reputation: 2581

ListView having dynamic views for each row

I want to display data in a ListView (For better performance and recycling) BUT each row of the ListView will have a vertical LinearLayout but is not guranteed that how many TextViews will be there in every LinearLayout.

Currently I am using the ViewHolder pattern and I am dynamically adding views to the LinearLayout in the getView() method of ListView. Note: I am calling linearLayout.removeAllViews() before adding views to it so as to eliminate duplicate views in LinearLayout.

Prolem The performance is very slow due to adding removing and adding dynamic views in ListView.

Any other idea how I can handle this situation? Shall I prefer a ScrollView and a vertical LinearLayout instead of ListView?

Please Help.

Let me know if any further clarification is required from my side.

Upvotes: 2

Views: 1333

Answers (1)

Wenger
Wenger

Reputation: 989

If you're experiencing slow performance, it might behoove you to get rid of as much processing in the getView() method as possible. getView() is called every time an item is created (new or recycled) and removing all views and redrawing them kind of defeats the purpose of recycling.

If you can parse through your data and simplify it before sending it to the adapter, that might take some load off the getView() method and help your performance issues.

EDIT: Like M-WaJeEh said above, controlling stuff with Visibility.VISIBLE and Visibility.GONE usually works better in my experience than changing the number of things that get inflated.

Upvotes: 2

Related Questions