我有一个的LinearLayout
认为已经包含几个要素。我想补充了很多意见吧,编程。因为这是在滚动型
,一切都将滚动。
I have a LinearLayout
view that already contains several elements. I want to add a lot more Views to it, programmatically. And because this is inside a ScrollView
, everything will be scrolled.
因此,我要做的就是通过我的列表,并添加我的自定义视图的新实例吧。这种自定义视图膨胀一个XML布局,并增加了一些方法。
So what I do is go through my list, and add new instances of my custom View to it. That custom view inflates a XML layout and adds a few methods.
此方法非常有效。现在的问题是,它的超慢,即使没有任何疯狂的code ...... 10个项目的列表需要大约500ms的实例。作为一个用户体验的角度来看,这是难以下咽。
This approach works well. The problem is that it's super slow, even without any crazy code... a list with 10 items takes around 500ms to instantiate. As an user experience standpoint, this is hard to swallow.
我的问题是,这是正确的/最好的方法?看来Android采取了大量的时间膨胀的布局,即使R.layout.my_list_item是超级简单。我不知道是否有一种方法,也许重用虚增的布局更多的意见,还挺缓存更复杂的分析?
My question is, is this the correct/best approach? Android seems to take a lot of time inflating the layout, even though "R.layout.my_list_item" is super simple. I wonder if there's a way to maybe to reuse "inflated" layouts for additional views, kinda caching the more complex parsing?
我已经试过这样做有一个的ListView
(和适配器和包装器),它似乎要快很多。问题是,我不能用一个简单的的ListView
;我的布局是不是一个简单的列表更复杂(的的LinearLayout
本身包含额外的自定义图标,它有另一名家长有更多的意见之前,它包裹的滚动型
)。
I've tried doing this with a ListView
(and adapter and a wrapper) and it seems to be much faster. The problem is that I can't use a simple ListView
; my layout is more complex than a simple list (the LinearLayout
itself contains additional custom icons, and it has another parent with even more Views before it's wrapped by the ScrollView
).
但是,有没有办法使用的适配器的的LinearLayout?那是不是比试图增加自己的看法更快?
But is there a way to use an adapter for a LinearLayout? Would that be faster than trying to add the views myself?
任何帮助是AP preciated。我很想让这个更快。
Any help is appreciated. I'd love to make this faster.
code如下:
主要活动:
// The LinearLayout that will contain everything
lineList = (LinearLayout) findViewById(R.id.lineList);
// Add a lot of items for testing
for (int i = 0; i < 10; i++) {
addListItem("Item number " + i);
}
protected void addListItem(String __title) {
MyListItem li;
li = new MyListItem(this);
li.setTitle(__title);
lineList.addView(li);
}
MyListItem:
public class MyListItem extends RelativeLayout {
protected TextView textTitle;
public MyListItem(Context __context) {
super(__context);
init();
}
public MyListItem(Context __context, AttributeSet __attrs) {
super(__context, __attrs);
init();
}
public MyListItem(Context __context, AttributeSet __attrs, int __attrsdefStyle) {
super(__context, __attrs, __attrsdefStyle);
init();
}
protected void init() {
// Inflate the XML layout
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_list_item, this);
// Create references
textTitle = (TextView)findViewById(R.id.textTitle);
}
public void setTitle(String __text) {
textTitle.setText(__text);
}
}
我想要做到的是这一点。考虑这个布局:
What I'm trying to accomplish is this. Consider this layout:
这布局是一个的FrameLayout
(外包装盒)含的ImageView
(灰色),一个的TextView
(内部矩形,顶部)和的LinearLayout
(内部矩形,在底部)。这的LinearLayout
矩形是一个我用动态的几个项目填充。
This layout is a FrameLayout
(outer box) containing a ImageView
(in gray), a TextView
(inner rectangle, on top) and a LinearLayout
(inner rectangle, on bottom). This LinearLayout
rectangle is the one I'm dynamically populating with a few items.
在我填充它,我想最后的结果是这样的(在每一个新的矩形是一个新的 MyListItem
)实例:
After I populate it, I want the final result to be this (where every new rectangle is a new MyListItem
instance):
即,一切是可滚动(背景图像,例如,被对准在顶部)。该的LinearLayout
本身不是滚动(一切如下),因此为什么的ListView
,据我所知,不会告发'T工作得很好
在我的情况。
That is, everything is scrollable (the background image, for example, is aligned on top). The LinearLayout
isn't scrollable by itself (everything else follows) hence why a ListView
, from what I know, wouldn't work very well
in my case.
推荐答案
3选项:
替换一切与一个ListView,与其他家长和自定义图标作为ListView的标头视图。 ListView的速度更快,因为它只是因为它需要他们创造的思考。
Replace everything with a ListView, with the other parent and custom icons as a header view for the ListView. ListView is faster, because it only creates Views as it needs them.
编程方式创建充气的my_list_item的内容相反,可能会更快
Programatically create the contents of my_list_item instead of inflating, might be quicker
使用 ViewStubs 可能让你加载按需意见。
Use of ViewStubs may allow you to load views on-demand.
也许它没有加载的意见,但该数据?在这种情况下,prepare在后台线程中的数据。
Maybe it isn't loading the views but the data? in which case prepare the data in a background thread.
相关推荐
最新文章