kylarsturn
kylarsturn

Reputation: 125

Setting a background resource for many linearlayouts in scrollview causes lag

I am dynamically adding around 150 linearlayouts to a scrollview in a grid-like layout. If I set the background resource to a drawable for each of them using setBackgroundResource(R.drawable.x), the scrollview shows extremely noticeable lag and choppiness, even though the drawable is a simple colour and border.

If I remove the call to setBackgroundResource, the scrollview is smooth again.

Is this expected to happen with so many views containing backgrounds? If so, how would I go about making a grid with custom backgrounds for each cell?

Upvotes: 0

Views: 410

Answers (2)

JustSoAmazing
JustSoAmazing

Reputation: 747

It seems like you're trying to create your own list view implementation so that you can set your own layouts for each row. I don't recommend doing this. Instead, use the default list view implementation provided by Android and instead of setting a default ArrayAdapter instance on the list view, subclass ArrayAdapter, override the getView method, and return your custom layout.

I highly recommend you check out this tutorial for a more thorough explanation:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Upvotes: 0

Nathaniel D. Waggoner
Nathaniel D. Waggoner

Reputation: 2886

You're going to want to use a list view in your scroll, and you're going to want to use a ListAdapater:

http://developer.android.com/guide/topics/ui/layout/listview.html

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

Basically what's going on is you're loading a large number of images into memory, and the scroll view by default doesn't do a very good job of managing releasing and inflating these resources.

Using methods similar to the above, with some custom image management, I've managed to get thousands of views running smoothly on a scroll.

Upvotes: 1

Related Questions