How do I clone a View?
Asked Answered
S

1

73

My question is almost exactly this question:

Clone textview to append it to a ViewGroup

However, I was inflating a view, and then attempting to clone it at the object level for performance reasons (I don't want to parse XML every single time), so that answer doesn't help me. View.clone() is protected and it apparently doesn't have a copy constructor. Is there any way to do this?

Siskind answered 11/11, 2010 at 20:59 Comment(5)
Are you sure that it would be significantly faster to clone it at the object level than to inflate it twice? What is guiding that assertion?Spermatophyte
I suppose I phrased that wrong, it's more like I wanted to see if it would be faster.Siskind
If you are creating a lot of instances of a particular view, you should probably be using a ListView or some other element that will recycle view instances as you scroll.Spermatophyte
The problem is that I'm doing a more complicated layout inside of a scrollview. This problem was a result of abandoning that approach. For some reason, ListViews do not behave well with ScrollViews.Siskind
This thread is similar #3900544Charged
B
111

You cannot clone views, the way to do it is to inflate your View every time. Note that the XML is compiled into binary which can be parsed very efficiently.

Bittner answered 11/11, 2010 at 21:4 Comment(2)
I read somewhere today that inflating views is very expensive. Especially in lists.Contestation
@frostymarvelous, It's a lot more expensive (speed-wise) than re-using already created views (though that's not always an option), but there's only a very slight difference between inflating and programmatic creation of new Views.Sherysherye

© 2022 - 2024 — McMap. All rights reserved.