编辑:还有一条可能相关的信息:我看到问题的用例是选项卡切换。也就是说,我在选项卡 A 上创建视图 X,离开选项卡 A 时将其删除,然后将其回收到选项卡 B。这就是问题发生的时候。这也正是我需要性能提升的时候。. .
我正在研究我的 Android 应用程序的性能。我注意到我可以通过重用我们将称为 MyLayout 的类的 View 对象来加快速度。(它实际上是一个自定义 FrameLayout 子类,但这可能无关紧要。此外,这与 ListView 无关。)也就是说,当我完成一个 View 时,而不是让 GC 得到它,我把它放入一个水池。当同一个活动需要另一个 MyLayout 对象时,我会从池中抓取一个(如果有)。这确实加速了应用程序。但是我很难清除旧的尺寸信息。结果是,当我拿回 View 时,通常一切都很好,但在某些情况下,新 View 在使用新的尺寸信息布局之前会短暂出现。即使我在将视图添加回层次结构之前或之后不久设置了新的 LayoutParams,也会发生这种情况(我已经尝试了两种方法;都没有帮助)。因此,用户会看到旧尺寸的短暂(可能是 100 毫秒)闪烁,然后才会变为正确的尺寸。
我想知道我是否/如何解决这个问题。下面,通过 C#/Xamarin,是我尝试过的一些事情,但都没有帮助:
回收时:
//Get myLayoutParams, then:
myLayoutParams.Width = 0;
myLayoutParams.Height = 0;
this.SetMeasuredDimension(0, 0);
this.RequestLayout();
在带回之前或之后——在将布局添加到其新父级的同一事件循环中:
// 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;
我也试过像下面这样把它带回来,但问题仍然存在:
FrameLayout.LayoutParams layoutParams = . . . // the same LayoutParams as above
parent.AddView(viewThatIsBeingRecycled, layoutParams);
编辑:根据要求,我尝试过的一些序列。所有人都遭受同样的问题。基本问题是即使 LayoutParams 是正确的,但布局本身并不正确,因为实际布局尚未发生。
回收时间:
尝试A:
this.RemoveFromHierarchy();
// problem is that the width and height are retained
尝试 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.
重新添加视图时:
所有尝试都调用以下子例程之一以将 LayoutParams 同步到模型:
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;
}
尝试A:
newParent.AddView(viewThatIsBeingRecycled);
// get layoutParams of the view, then:
SyncExistingLayoutParamsToModel(myLayoutParams, model);
尝试 B:与 A 相同,但顺序相反:
// get layoutParams of the view, then:
SyncExistingLayoutParamsToModel(myLayoutParams, model);
newParent.AddView(viewThatIsBeingRecycled);
尝试 C:与 A 相同,但使用新的 layoutParams:
newParent.AddView(viewThatIsBeingRecycled);
FrameLayout.LayoutParams layoutParams = CreateLayoutParamsFromModel(model);
viewThatIsBeingRecycled.LayoutParams = layoutParams;
尝试 D:与 B 相同,但使用新的 layoutParams:
FrameLayout.LayoutParams layoutParams = CreateLayoutParamsFromModel(model);
viewThatIsBeingRecycled.LayoutParams = layoutParams;
newParent.AddView(viewThatIsBeingRecycled);
尝试 E:使用带有 layoutParams 参数的 AddView:
FrameLayout.LayoutParams layoutParams = CreateLayoutParamsFromModel(model);
newParent.AddView(viewThatIsBeingRecycled, layoutParams);
在所有五种情况下,问题是即使 layoutParams 是正确的,在布局调整到新的 layoutParams 之前视图对用户可见。