Android的动态创建的表 - 糟糕的表现 [英] Android dynamically created table - bad performance

查看:168
本文介绍了Android的动态创建的表 - 糟糕的表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想请你帮助我。
 我要动态地创建表(截图)。我通过创建它低于code:

i would like you to help me. I want to create dynamicly table (screenshot). I created it via code below:

    TableLayout tl = (TableLayout) findViewById(R.id.main_table);

    FOR. 1-300.........
    TableRow tr_head = new TableRow(this);
    tr_head.setId(10);
    tr_head.setBackgroundColor(Color.CYAN);
    tr_head.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    RelativeLayout rl = new RelativeLayout(this);
    rl.setId(20);


    ImageButton xyz = new ImageButton(this);
    xyz.setId(21);
    xyz.setPadding(5, 5, 5, 5);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 20 );
    rl.addView(xyz,params); 

    tr_head.addView(rl);
    tl.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
    END OF FOR.......  

通过类似$ C C I好听创建2种类型的项目之一为类别(3子视图),一个用于类项目(10子视图)的。
然后,我用code波纹管分配按钮和整个项目的onclick监听器:

Via similar code i nicely create 2 types of items one for category(3 child views) and one for category item(10 child views). Then i use code bellow to assign onclick listener for buttons and whole items.:

int count = tl.getChildCount();
    for(int i = 0; i < count; i++){
        TableRow v = (TableRow)tl.getChildAt(i);

        if(v.getChildAt(0) instanceof RelativeLayout){
            RelativeLayout relativ = (RelativeLayout)v.getChildAt(0);

        if(relativ.getChildCount()>5)
            relativ.setOnClickListener(new MyClickListener());
                     ...........

但是,当我想创建表,其中包含300项,它需要30秒。呈现在模拟器这一观点。这实在是很慢的。所以,我想问问你如何呈现这一观点。一些示例或教程将是非常有益的。

But when i want to create table which contains 300 items, it takes 30 sec. to render this view on emulator. It is really very slow. So i would like to ask you how to render this view. Some example or tutorial will be very helpfull.

提前非常感谢。

推荐答案

Android是在有记忆很多意见很慢。要解决这一点,我会建议使用默认的Andoird的ListView用自定义ListAdapter。

Android is very slow at having many Views in memory. To work around this I would recommend using the default Andoird ListView with a custom ListAdapter.

的意见是动态创建当用户滚动列表,因此只有当前可见的观点,必须在内存中。

The Views are created on the fly when the user scrolls the list, so only the currently visible Views have to be in memory.

此示例使用CursorAdapter的,但你也可以使用一个ArrayAdapter。

This example uses a CursorAdapter, but you can also use an ArrayAdapter.

private class ExtendCursorAdapter extends CursorAdapter {

    public ExtendCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) { //Determine if it's a category or an item
            return 0; // category
        } else {
            return 1; // item
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == 0) {
            View v;
            if (convertView != null)
                v = convertView;
            else
                v = inflater.inflate(R.id.listcategory);
            // Set title, ...
            return v;
        } else{
            // the same for the item
        }
    }
}

这是额外的性能的提高是通过convertView的用法。在翻滚时并不需要创建任何额外的意见,因为Android的重用哪来的视线之外的人。你只需要确保将所有数据重置convertView的。

An additional performance increase comes from the usage of convertView. While scrolling you don't need to create any additional Views because Android reuses the ones which come out of sight. You just have to make sure to reset all data of convertView.

这篇关于Android的动态创建的表 - 糟糕的表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆