2015-11-04 44 views
0

我有一個包含TableLayout的Dialogfragment。我用一個自定義類包裝了TableLayout(下面給出),因爲我想要一個固定的頭文件和一個滾動的主體。我希望頭部與身體正確對齊。自定義類通過覆蓋onLayout來完成此操作。調用requestLayout時可見的緩慢

  <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而調整大小的列。有沒有什麼辦法解決這一問題 ?

+0

將附加代碼移到另一個線程(UI)! –

回答

0

由於Nguyen建議我轉移到另一個線程。但是我刪除了requestLayout。相反,在新的線程中,我使用了下面這個似乎可以做到的伎倆。

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