【发布时间】:2013-11-27 11:46:41
【问题描述】:
我需要创建一个表格布局,其中标题行不会是滚动视图的一部分,但整个布局应该是水平滚动视图的一部分。我尝试了各种解决方案。一个是here ,另一个是here 。一切都很好。但就我而言,问题是我需要一切都是动态的。并且还有以下要求。
1) 使用固定标题垂直滚动表格。
2) 水平滚动整个视图。
3)点击每个标题,我需要对表格内容进行排序。即所有行都应该重新排列。这也有效,但标题和内容表的对齐都是徒劳的。
请查看链接,下面是我的布局 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tabsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:orientation="horizontal"
android:weightSum="100" >
</LinearLayout>
<HorizontalScrollView
android:id="@+id/contentHorizontalScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tabsLayout"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="2dp"
android:fillViewport="true" >
<com.myapp.uiutilities.ScrollingTable
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/leaderboardScrollingTable"
android:orientation="vertical">
<TableLayout
android:id="@+id/leaderboard_header_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:id="@+id/leaderboard_content_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TableLayout>
</ScrollView>
</com.myapp.uiutilities.ScrollingTable>
</HorizontalScrollView>
</RelativeLayout>
ScrollingTable是一个自定义的LinearLayout,代码是用onLayout方法写的,用来重新调整表格的列宽。 ScrollingTable.java 的代码如下。
public class ScrollingTable extends LinearLayout
{
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.leaderboard_header_table );
TableLayout body = (TableLayout) findViewById( R.id.leaderboard_content_table );
for ( int rownum = 0; rownum < body.getChildCount(); rownum++ )
{
TableRow row = (TableRow) body.getChildAt( rownum );
for ( int cellnum = 0; cellnum < row.getChildCount(); cellnum++ )
{
View cell = row.getChildAt( cellnum );
Integer cellWidth = cell.getWidth();
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 = colWidths.get(cellnum);
}
}
}
}
如果有人可以,请为我提供更好的解决方案。提前致谢。
【问题讨论】:
标签: android