Answer the question
In order to leave comments, you need to log in
AddView/removeAllChildren optimization for Android custom component?
Hello!
I am new to android development. I faced a rather simple task, which I coped with, but so far somehow inferior, because. the feeling that I'm either going the wrong way, or I need to optimize something corny, tk. lags.
The essence of the problem: there is an element in the list (ListView), it has a button. When the button is pressed, the state of this element changes and it shows another View in some part of itself (list element).
When changing the state, I pull removeAllChildren, and then addView of the View that I had previously created by LayoutInflator and cached.
The same thing happens when the Adapter pushes data through the getView call. And at this very moment when you need to recreate the children and there is a lag. If it occurs by pressing a button, that's nothing. But when scrolling, it occurs constantly, because. it is known that View representing list items in ListView are reused.
Dear Habr, please tell me a noob, how do intelligent Android developers do such things?
PS At first I thought that the point is that the list element has a complex hierarchy of layouts. I flattened it by replacing it with one RelativeLayout and throwing components into it. But this did not help, because. the point, I'm sure, is in removeAllChildren/addView
Answer the question
In order to leave comments, you need to log in
In the meantime, I seem to have found a solution.
It is necessary to create a separate View for each state and already in the Adapter itself, depending on the data, instantiate the required View, push the state change handler there. When the state changes, update the data element and pull the ListView update.
Wait for the advice of experienced developers, but for now ... have you tried to pre-create and place the necessary views on the list element and, when certain events occur (the same click on the button), only control their visibility (View.VISIBLE, View.INVISIBLE, View.GONE ) and changing the state (maybe there is some kind of filling or something else)? Or is it not suitable for you? In my opinion, this is how create-delete at runtime is not very good.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question