2014-10-07

2014-10-07 21:00
1
Androidで、TableLayoutをスクロールさせるためにはTableLayout全体をスクロールさせる方法しかないようで、
Headerを固定にし、Bodyのみスクロールさせるためには、Header部分とBody部分のTableLayoutを分離する必要があるようです。
例)


 
  
  
  
  
 



 

 


HeaderとBodyを分離した場合、Bodyの部分の内容が固定(もしくはCellのWidthが固定)の場合は問題ありませんが、
Bodyの内容が前の処理などによって動的に変わる且つ、CellのWidthも変わる場合はHeaderとBodyのCellが合わなくなります。

それを対処するためには、色々と方法があるかと思いますが、今回は以下の2つの方法について整理してみました。
1.LinearLayoutのonLayout時にTableLayoutのCellのWidthを調整する
2.RelativeLayoutに同じ内容のTableLayoutを二つ生成し、marginTopで位置をずらす。

1.LinearLayoutのonLayout時にTableLayoutのCellのWidthを調整する

詳細ソース:https://github.com/kongbab04/ListViewTable
layoutのmain.xml


 
 
  
   
    
    
    
    
   
   
    
    
    
    
   
  
  
  
   

   
  
  


ListViewTable.java
package com.android.listview.table;

import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;


public class ListViewTable extends LinearLayout {
 public ListViewTable( Context context ) {
  super( context );
 }

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

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  super.onLayout( changed, l, t, r, b );
  
  List colWidths = new LinkedList();

  TableLayout header = (TableLayout) findViewById( R.id.headerTable );
  TableLayout body = (TableLayout) findViewById( R.id.bodyTable );
  if (body.getChildCount() > 0) {
   
   for ( int rownum = 0; rownum < 1; rownum++ ) {
    TableRow row = (TableRow) body.getChildAt( rownum );
    for ( int cellnum = 0; cellnum < row.getChildCount(); cellnum++ ) {
     View cell = row.getChildAt( cellnum );
     TableRow.LayoutParams params = (TableRow.LayoutParams)cell.getLayoutParams();
     Integer cellWidth = params.span == 1 ? cell.getWidth() : 0;
     if ( colWidths.size() <= cellnum ) {
      colWidths.add( cellWidth );
     } else {
      Integer current = colWidths.get( cellnum );
      if( cellWidth > current ) {
       colWidths.remove( cellnum );
       colWidths.add( cellnum, cellWidth );
      }
     }
    }
   }
 
   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 = 0;
     for( int span = 0; span < params.span; span++ ) {
      params.width += colWidths.get( cellnum + span );
     }
    }
   }
  }
 }
}


結果のキャプチャー


詳細のソースコードはGithubをご参照ください。
https://github.com/kongbab04/ListViewTable


2.RelativeLayoutに同じ内容のTableLayoutを二つ生成し、marginTopで位置をずらす。

詳細ソース:https://github.com/kongbab04/ListViewTable2
layoutのmain.xml

    

 
 
  
   
   
   
   
  
  
   
   
   
   
  
  
   
   
   
   
  
  
   
   
   
   
  
  
   
   
   
   
  
  
   
   
   
   
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
   
   
   
   
  
  
  
 
 
 
 
  
  
   
   
    
    
    
    
   
   
    
    
    
    
   
   
    
    
    
    
   
   
    
    
    
    
   
   
    
    
    
    
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
   
   
    
    
    
    
   
  
 


style.xml


 
 
 
 


結果のキャプチャー



詳細のソースコードはGithubをご参照ください。
https://github.com/kongbab04/ListViewTable2


1 コメント:

  1. どうも
    LinearLayoutのonLayout時にWidthを調整する
    ↑この発想は思わなかった
    本当に助かった、ありがとう

    返信削除