Is it safe to create and modify views in a background thread before adding them to a ViewGroup?
I have many views in a linear layout to create. I have created a background thread that loops through all the data and creates an array list of views. At the end of the thread it calls runOnUIThread to loop through the array list and add each view to the linear layout.
Is this dangerous? The views aren't part of the hierarchy yet when I'm creating and manipulating them开发者_如何转开发 in the background thread. Initial testing hasn't yielded any problems.
(I am using a linear layout instead of list views, because my experience with list views is that they try to reuse some of their elements for multiple rows. For example, a checkbox that you check becomes unchecked again if it scrolls off the screen and back on. This seems simpler. Maybe I'm taking a performance hit, though. Still need to check this thoroughly.)
It shouldn't cause any problems. You are not doing any change to the UI until it's really necessary, which is a good thing.
BUT...
because my experience with list views is that they try to reuse some of their elements for multiple rows. For example, a checkbox that you check becomes unchecked again if it scrolls off the screen and back on
You should try to do it using ListView
s which will increase the performance. IMO, if you don't know how to use a ListView and its recycling system, that's not a good excuse to not use it at all; so, just try to read about that and give it a try. How hard could it be? You don't have to be Einstein to get that to work :)
精彩评论