EDIT: One more piece of possibly relevant info: The use case in which I see the problem is tab switching. That is, I create view X on tab A, remove it when leaving tab A, then recycle it into tab B. That's when the problem happens. This is also exactly when I need the performance gain . . .
I am working on the performance of my Android app. I've noticed that I can speed things up by reusing View objects of a class we'll call MyLayout. (It's actually a custom FrameLayout subclass, but that likely doesn't matter. Also, this is NOT ListView related.) That is, when I'm done with a View, rather than letting GC get ahold of it, I put it into a pool. When the same activity wants another MyLayout object, I grab one from the pool, if available. This does indeed speed up the app. But I'm having a hard time clearing the old size information. The result is that when I grab the View back, things are usually fine, but in some cases, the new View briefly appears before it is laid out with the new size information. This happens even though I set new LayoutParams shortly before or after adding the View back into the hierarchy (I've tried both ways; neither helps). So the user sees a brief (maybe 100ms) flash of the old size, before it goes to the correct size.
I'm wondering if/how I can work around this. Below, via C#/Xamarin, are some things I've tried, none of which help:
When recycling:
//Get myLayoutParams, then:
myLayoutParams.Width = 0;
myLayoutParams.Height = 0;
this.SetMeasuredDimension(0, 0);
this.RequestLayout();
Immediately before or after bringing back -- within the same event loop that adds the layout to its new parent:
// a model object has already computed the desired x, y, width, and height
// It's taken into account screen size and the like; the Model's sizes
// are definitely what I want.
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams (model.width, model.height);
layoutParams.LeftMargin = model.x;
layoutParams.TopMargin = model.y;
this.LayoutParameters = layoutParams;
I've also tried bringing it back like the below, but the problem still remains:
FrameLayout.LayoutParams layoutParams = . . . // the same LayoutParams as above
parent.AddView(viewThatIsBeingRecycled, layoutParams);
EDIT: Per request, some of the sequences I have tried. All suffer from the same problem. The basic issue is that even though the LayoutParams are correct, the layout itself is not correct as the actual layout has not happened yet.
Recycling time:
attempt A:
this.RemoveFromHierarchy();
// problem is that the width and height are retained
attempt B:
//Get myLayoutParams, then:
myLayoutParams.Width = 0;
myLayoutParams.Height = 0;
this.SetMeasuredDimension(0, 0);
this.RequestLayout();
this.RemoveFromHierarchy();
//problem is that even though layout has been requested, it does not actually happen.
//Android seems to decide that since the view is no longer in the hierarchy,
//it doesn't need to do the actual layout. So the width and height
//remain, just as they do in attempt A above.
When adding the view back:
All attempts call one of the following subroutine to sync the LayoutParams to the model:
public static void SyncExistingLayoutParamsToModel(FrameLayout.LayoutParams layoutParams, Model model) {
layoutParams.TopMargin = model.X;
layoutParams.LeftMargin = model.Y;
layoutParams.Width = model.Width;
layoutParams.Height = model.Height;
}
public static FrameLayout.LayoutParams CreateLayoutParamsFromModel(Model model) {
FrameLayout.LayoutParams r = new FrameLayout.LayoutParams(model.Width, model.Height);
r.LeftMargin = x;
r.TopMargin = y;
return r;
}
Attempt A:
newParent.AddView(viewThatIsBeingRecycled);
// get layoutParams of the view, then:
SyncExistingLayoutParamsToModel(myLayoutParams, model);
Attempt B: same as A, but in the opposite order:
// get layoutParams of the view, then:
SyncExistingLayoutParamsToModel(myLayoutParams, model);
newParent.AddView(viewThatIsBeingRecycled);
Attempt C: same as A, but with fresh layoutParams:
newParent.AddView(viewThatIsBeingRecycled);
FrameLayout.LayoutParams layoutParams = CreateLayoutParamsFromModel(model);
viewThatIsBeingRecycled.LayoutParams = layoutParams;
Attempt D: same as B, but with fresh layoutParams:
FrameLayout.LayoutParams layoutParams = CreateLayoutParamsFromModel(model);
viewThatIsBeingRecycled.LayoutParams = layoutParams;
newParent.AddView(viewThatIsBeingRecycled);
Attempt E: using the AddView that takes a layoutParams argument:
FrameLayout.LayoutParams layoutParams = CreateLayoutParamsFromModel(model);
newParent.AddView(viewThatIsBeingRecycled, layoutParams);
In all five cases, the problem is that even though the layoutParams are correct, the view is made visible to the user before the layout adjusts itself to the new layoutParams.
forceLayout
instead ofrequestLayout
when you're about to add the view. – AdornMyLayout
object with just the stockFrameLayout
? Probably just commenting out everything in the class so it will behave just like its parent class would be sufficient. I am hoping it will help us to establish that the issue is not triggered by something special that you did in the class. – Roentgenoscope