Android TableLayout with over 1000 rows loading very slowly

心已入冬 提交于 2019-12-06 09:46:27

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.

Just from guessing on the method name, it seems this line may be slow to run on the main/UI thread: Methods.createRow(table, rooms.get(inx), null, activity);

I would suggest separating all the heavy database work and UI work with AsyncTask, so it might look something like this:

private OnClickListener rowClickListener = new OnClickListener()
{
    @Override
    public void onClick(View arg0) 
    {
        if (arg0.getTag() != null && arg0.getTag().getClass() == Integer.class)
            select((Integer)arg0.getTag());
    }
};

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

    final Handler handler = new Handler();

    new AsyncTask<Void, Void, List<TableRow>>() {
        @Override
        protected List<TableRow> doInBackground(Void... params) {
            // Do all heavy work here
            final List<TableRow> rows = new ArrayList<TableRow>(rooms.size());
            for (int x = 0; x < rooms.size(); x++)
            {
                Methods.createRow(table, rooms.get(x), null, activity);
                rows.add((TableRow)table.getChildAt(x));
            }
            return rows;
        }

        @Override
        protected void onPreExecute() {
            // maybe show progress indication
        }

        @Override
        protected void onPostExecute(List<TableRow> result) {
            // Do all UI related actions here (maybe hide progress indication)
            for (final TableRow row : result) {
                row.setOnClickListener(rowClickListener);
            }
        }
    };
}

Since I can't tell what is in some methods, you'll just need to ensure you've tried to optimize as best as possible in Methods.createRow(...) and move all the UI related work to the onPostExecute(...) method.

You are stacking the post with all the actions at once. So it same as not using a thread at all as your main thread doing all the work. Try changing to postDelay, and for each room index give it 1 millisecond or more.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!