2

We have an data-driven activity which constructs a large set (typically, up to 100) of Button-like components and places them in a Scrollable. Each of the buttons is inflated from a resource and are based on a RelativeLayout. The button has two text views, two image views - both from resources - and has a background of a 9-patch.

The size, position, text, and configuration of the image views are all driven from a database query.

Unfortunately, this is taking between 2-3 seconds to layout on an HTC Desire, 3-5 seconds on an HTC San Francisco or HTC Sapphire.

Once the initial layout is done, the view performs beautifully.

We have tried a number of strategies which made no difference:

  • time the database query - this was not a significant part of the delay.
  • cache the buttons, to reduce the amount of layout inflation.
  • constructing a subtree of views outside of the “live” view hierarchy, then connecting it when complete.
  • doing the same on another thread, but connecting the subtree to the activity on the UI thread.

We have an indefinite progress indicator (spinner/throbber) which spins while the query runs on another thread, but then freezes when layout gets going.

So, my question are these:

  • how can I make the layout of the views seem more responsive or
  • how can I avoid the thobber locking up while layout occurs

Thanks.

Edit

The scroller is set to scroll horizontally and vertically. So we have a grid which the screen is a viewport on.

This makes using the built in ListView (at first glance) unsuitable for the task.

jamesh
  • 19,863
  • 14
  • 56
  • 96
  • I used this to improve responsiveness http://stackoverflow.com/questions/2293488/improve-listview-efficiency-when-loading-images-from-sd-into-the-listview – GPSmaster May 25 '11 at 22:32

1 Answers1

0

The progress indicator will not redraw during layout, because it's all happening in the UI thread.

To improve performance you should use a ListView, it's scrollable and the items can be customized (you can reuse your RelativeLayout based "button") with a custom adapter, and it allows to recycle the items while scrolling. See Recycling views in a listview, worth it?, and above all this excerpt from CommonsWare's book on Android.

Community
  • 1
  • 1
bigstones
  • 15,087
  • 7
  • 65
  • 82
  • The list view isn't suitable for the data, as the grid needs to be scrolled horizontally as well as vertically. – jamesh May 26 '11 at 09:05