Android 动态创建的表 - 性能不佳

标签 android android-layout tablelayout

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

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

通过类似的代码,我很好地创建了两种类型的项目,一种用于类别(3 个 subview ),另一种用于类别项目(10 个 subview )。 然后我使用下面的代码为按钮和整个项目分配 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 秒。在模拟器上呈现此 View 。它真的很慢。所以我想问你如何渲染这个 View 。一些示例或教程将非常有帮助。

非常感谢。

I want to create this

最佳答案

Android 在内存中有很多 View 时速度非常慢。要解决此问题,我建议使用带有自定义 ListAdapter 的默认 Andoird ListView。

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

此示例使用 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 的使用。滚动时您不需要创建任何额外的 View ,因为 Android 会重用那些看不见的 View 。您只需确保重置 convertView 的所有数据。

关于Android 动态创建的表 - 性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9813427/

相关文章:

java - 究竟是什么导致了这个 NullPointerException?

Android 以整数和浮点形式显示复数

android - 带有动态按钮android的扩展列表中的计时器

android - 如何在选项卡项中添加 Textview?我在 setCustomView 中得到 nullPointerException

android - 在 Android 应用程序中抽象 Realm 的正确方法

Android WifiManager - 停止扫描

android - 在布局底部使用 EditText 进行调整平移会导致软键盘稍微遮挡 EditText View

android - 带有表格布局的 ListView

android - 对齐 TableRow 内的按钮

Android关于TableLayout和省略号的问题