调用requestLayout时可见速度慢

时间:2015-11-04 23:50:34

标签: android android-linearlayout android-tablelayout

我有一个包含TableLayout的Dialogfragment。我已经使用自定义类(下面给出)包装了TableLayout,因为我想要一个固定的标题和一个滚动的主体。我希望标题与身体正确对齐。自定义类通过覆盖onLayout来完成此任务。

            <com.ui.components.ScrollingTable
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_weight="1"
                android:background="@color/transaction_table_bg">

                <TableLayout
                    android:id="@+id/tlScrollingTableHeader"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbarStyle="outsideOverlay"
                    android:fillViewport="true">

                    <TableLayout
                        android:id="@+id/tlScrollingTableBody"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

                </ScrollView>

            </com.ui.components.ScrollingTable>

我使用以下类来包装TableLayout。

public class ScrollingTable extends LinearLayout {

    private static final String TAG = ScrollingTable.class.getSimpleName();

    public ScrollingTable (Context context) {
        super(context);
    }

    public ScrollingTable (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        super.onLayout(changed, l, t, r, b);

        List<Integer> colWidths = new LinkedList<Integer>();

        TableLayout header = (TableLayout) findViewById(R.id.tlScrollingTableHeader);
        TableLayout body = (TableLayout) findViewById(R.id.tlScrollingTableBody);

        // Measure content width first
        for (int rownum = 0; rownum < body.getChildCount(); rownum++) {
            TableRow row = (TableRow) body.getChildAt(rownum);
            int countCells = 0;
            for (int cellnum = 0; cellnum < row.getChildCount(); cellnum++) {
                View cell = row.getChildAt(cellnum);
                if (cell.getVisibility() == VISIBLE) {
                    Integer cellWidth = cell.getWidth();
                    if (colWidths.size() <= countCells) {
                        colWidths.add(cellWidth);
                    } else {
                        Integer current = colWidths.get(countCells);
                        if (cellWidth > current) {
                            colWidths.remove(countCells);
                            colWidths.add(countCells, cellWidth);
                        }
                    }
                    countCells++;
                }
            }
        }

        // Figure out if header needs resizing first based on widths
        TableRow headerRow = (TableRow) header.getChildAt(0);
        for (int count = 0; count < colWidths.size(); count++) {
            if (headerRow.getChildAt(count).getWidth() >= colWidths.get(count)) {
                colWidths.remove(count);
                colWidths.add(count, headerRow.getChildAt(count).getWidth());
            }
        }

        // Then apply to header
        for (int rownum = 0; rownum < header.getChildCount(); rownum++) {
            TableRow row = (TableRow) header.getChildAt(rownum);
            for (int cellnum = 0; cellnum < row.getChildCount(); cellnum++) {
                View cell = row.getChildAt(cellnum);
                TableRow.LayoutParams params = (TableRow.LayoutParams) cell.getLayoutParams();
                params.width = colWidths.get(cellnum);
                cell.requestLayout();
            }
        }
    }
}

除了一个问题之外,这很有效。对话框打开后,我可以清楚地看到由于requestLayout而调整大小的列。无论如何要解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

Nguyen建议我转移到另一个线程。但是我删除了requestLayout。相反,在新线程中我使用了以下似乎可以解决问题的方法。

    @Override
    protected void onPostExecute(Void result) {
        tableHeader.setColumnCollapsed(0, false);
        tableContent.setColumnCollapsed(0, false);
    }