Android的TableLayout超过1000行的载入速度很慢 [英] Android TableLayout with over 1000 rows loading very slowly

查看:299
本文介绍了Android的TableLayout超过1000行的载入速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来加快与超过1000行创建一个TableLayout的。有没有办法在一个单独的线程或方式来加快它完全是建立一个TableLayout?

下面是我的方法是创建表:

 私人无效可设置()
{
    最后活动活动=这一点;    最后的处理程序处理程序=新的处理程序();    新主题(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
            为(中间体X = 0; X&下; rooms.size(); X ++)
            {
                最终诠释INX = X;
                handler.post(新的Runnable()
                {
                    @覆盖
                    公共无效的run()
                    {
                        Methods.createRow(表,rooms.get(INX),空,活动);
                        的TableRow行=(的TableRow)table.getChildAt(INX);
                        row.setOnClickListener(新OnClickListener()
                        {
                            @覆盖
                            公共无效的onClick(查看arg0中)
                            {
                                如果(arg0.getTag()= NULL和放大器;!&安培; arg0.getTag()的getClass()== Integer.class)
                                    选择((整数)arg0.getTag());
                            }
                        });
                    }
                });
            }
        }    })。开始();
}

我希望通过处理器至少会允许创建表之前,新的活动出现。应用程序似乎创建具有很多行表时,冻结为几秒钟。可设置()是在我的活动的在onStart()方法中运行。

Methods.createRow添加一行到在传递的TableView的端

编辑:

决定尝试一个ListView之后,我少了很多code更好的结果。

 私人无效可设置()
{
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,roomNames);
    table.setAdapter(适配器);
    table.setOnItemClickListener(新OnItemClickListener()
    {
        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3)
        {
            选择(ARG2);
        }
    });
}


解决方案

首先第一件事情。

为什么您的应用程序冻结:
处理程序就像一个队列后,它会将你做每一个岗位,比你的主线程连续执行它。

但主要的问题是你想一次显示的数据量,但很容易通过一个适配器解决了,你大概可以使用一些默认组件为解决这个问题,像的 ListView控件或的 GridView控件,你可以让你的自定义行的工作也许是列左右。

I'm looking for a way to speed up the creation of a TableLayout with over 1000 rows. Is there a way to create a TableLayout entirely on a separate thread or a way to speed it up?

Here is my method that is creating the table:

private void setTable()
{       
    final Activity activity = this;

    final Handler handler = new Handler();

    new Thread(new Runnable()
    {
        @Override
        public void run() 
        {
            for (int x = 0; x < rooms.size(); x++)
            {
                final int inx = x;
                handler.post(new Runnable()
                {
                    @Override
                    public void run() 
                    {
                        Methods.createRow(table, rooms.get(inx), null, activity);
                        TableRow row = (TableRow)table.getChildAt(inx);
                        row.setOnClickListener(new OnClickListener()
                        {
                            @Override
                            public void onClick(View arg0) 
                            {
                                if (arg0.getTag() != null && arg0.getTag().getClass() == Integer.class)
                                    select((Integer)arg0.getTag());
                            }
                        });
                    }
                });
            }
        }

    }).start();
}

I was hoping that using a Handler would at least allow the new Activity to appear before the table was created. The application seems to freeze up for a few seconds when creating tables with a lot of rows. setTable() is being run in my Activity's onStart() method.

Methods.createRow adds a row to the end of the TableView that is passed in.

Edit:

After deciding to try out a ListView, I got much better results with a lot less code.

private void setTable()
{
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, roomNames);
    table.setAdapter(adapter);
    table.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
        {
            select(arg2);
        }
    });
}

解决方案

First things first.

Why your app freezes: Handler works like a queue, it queues every post you made and than execute it serially in your main thread.

But the main problem is the amount of data you are trying to show at once, but it is easily solved with an Adapter, you probably can use some default Component for solve this, like ListView or GridView, you can make your custom rows to work around the columns maybe.

这篇关于Android的TableLayout超过1000行的载入速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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