【问题标题】:Android dynamically created table - bad performanceAndroid动态创建表 - 性能不佳
【发布时间】:2012-04-06 11:48:06
【问题描述】:

我希望你能帮助我。 我想动态创建表格(截图)。我通过下面的代码创建了它:

    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.......  

通过类似的代码,我很好地创建了 2 种类型的项目,一种用于类别(3 个子视图),另一种用于类别项目(10 个子视图)。 然后我使用下面的代码为按钮和整个项目分配 onclick 侦听器。:

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 秒。在模拟器上呈现此视图。它真的很慢。所以我想问你如何渲染这个视图。一些示例或教程会很有帮助。

非常感谢。

【问题讨论】:

    标签: android android-layout tablelayout


    【解决方案1】:

    Android 在内存中有很多视图时速度很慢。为了解决这个问题,我建议使用带有自定义 ListAdapter 的默认 Andoird ListView。

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

    此示例使用 CursorAdapter,但您也可以使用 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 的所有数据。

    【讨论】:

      【解决方案2】:

      我遇到了类似的性能问题,除了我想做的只是以格式化的表格形状(自动调整大小的列等)显示文本。在这种情况下,您可能希望完全放弃 TableLayout 并使用 java.util.Formatter 之类的东西将文本转换为表格形状并将其提供给单个 TextView。对我来说,这带来了巨大的性能提升(大约 2 秒即可将活动加载到几乎即时)。

      查看更多详情here

      【讨论】:

        猜你喜欢
        • 2015-04-27
        • 2017-12-25
        • 1970-01-01
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 2015-11-15
        相关资源
        最近更新 更多