[英]What is the best way to add views to layout from code in android?
I have loop
in my code to add Views
to the Layout
, the following code is a Loop which is present in another loop
and i have so many loops like this, Activity takes so much time to start
how to handle this ? 我有
loop
在我的代码添加Views
的Layout
,下面的代码是一个Loop which is present in another loop
,我有这么多圈就是这样, Activity takes so much time to start
该如何处理呢?
foreach (InfoQuery item in InfoList)
{
if (item == "kitten")
{
if (!done)
{
TextView view= new TextView(ApplicationContext);
view.LayoutParameters = mainLayout.LayoutParameters;
view.TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, 3, ApplicationContext.Resources.DisplayMetrics);
view.SetPadding((int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 10, ApplicationContext.Resources.DisplayMetrics), 0, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 10, ApplicationContext.Resources.DisplayMetrics), 0);
view.Text = item.position;
layout2.AddView(view,0);
done = true;
}
TextView view2= new TextView(ApplicationContext);
view2.LayoutParameters = mainLayout.LayoutParameters;
view2.TextSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, 3, ApplicationContext.Resources.DisplayMetrics); ;
view2.SetPadding((int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 30, ApplicationContext.Resources.DisplayMetrics), 0, (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 10, ApplicationContext.Resources.DisplayMetrics), 0);
view2.SetTextColor(Color.Black);
view2.Text = item.position;
layout2.AddView(view2);
}
}
You shouldn't be adding a giant set of items to a layout in Android. 您不应该在Android布局中添加大量项目。 Use a recyclerview or listview instead.
请改用recyclerview或listview。 Adding all the views, in addition to being slow, takes a ton of RAM, and a lot of time to draw even though it isn't on screen.
除了很慢之外,添加所有视图还需要大量的RAM,并且即使不在屏幕上,也需要大量的绘制时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.