CursorAdapter slow/jerky scroll

2019-05-12 17:53发布

I've got a ListActivity that's using a CursorAdapter to draw a dataset in a table format. I originally used a TableLayout (and drew the entire table) but I was concerned that this was using too much memory (and drawing the page was slow) so I'm now investigating using a CursorAdapter.

The XML layout for the body of the activity looks like this (note the use of a HorizontalScrollView, which may be a cause):

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layout_query_viewer">

    <TextView
        android:id="@+id/query_table_name"
        style="@style/ActivityTitle" />

    <HorizontalScrollView
        android:id="@+id/hsvMainContent"
        android:scrollbars="none"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:id="@+id/llMainContent"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/fixed_headers"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@android:id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

The CursorAdapter looks like this:

private class QueryAdapter extends CursorAdapter {
    public QueryAdapter(Context context) {
        super(context, null);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        /*
         * I'm deliberately not inflating here as I'm synchronizing a remote
         * database, so I don't know how many columns my query will return.
         */
        LinearLayout layout = new LinearLayout(context);
        ViewHolder holder = new ViewHolder(cursor.getColumnCount());
        layout.setTag(holder);

        for (int i = 1; i < cursor.getColumnCount(); i++) {
            TextView cell = new TextView(context);

            holder.cells[i] = cell;

            cell.setBackgroundColor(Color.WHITE);
            cell.setMinimumWidth(mColumnWidths[i]);
            cell.setPadding(1, 1, 1, 1);

            layout.addView(cell);
        }

        return layout;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder)view.getTag();

        for (int i = 1; i < cursor.getColumnCount(); i++) {
            holder.cells[i].setText(cursor.getString(i));
        }
    }
}

static class ViewHolder {
    TextView[] cells;

    public ViewHolder(int size) {
        cells = new TextView[size];
    }
}

I was happy with the scrolling performance when I drew the entire table but now that I've switched to the CursorAdapter the scrolling is jerky (and I mean unacceptably jerky). The CursorAdapter approach seems logically the correct decision architecturally but unless I can fix the scroll problem then I'll have to go back to drawing the entire table.

Has anyone else experienced this or can shed some light on what I doing wrong?

1条回答
等我变得足够好
2楼-- · 2019-05-12 18:28

I downloaded the "SQLite Editor" (by Speed Software) from the Android Market and noticed that their table rendering was very slick. I contacted the developers and they were kind enough to point me in the right direction.

I actually ended up using a canvas and drawing the table in a custom view. It didn't take much longer than implementing the CursorAdapter solution.

查看更多
登录 后发表回答