0
我需要创建一个表格布局,其中标题行不是滚动视图的一部分,但整个布局应该是水平滚动视图的一部分。我试过各种解决方案。一个是here另一个是here。一切运作良好。但我的情况是,我需要一切都是动态的。还有以下要求。
1)使用固定标题垂直滚动表格。
2)水平滚动整个视图。
3)点击每个标题,我需要对表格内容进行排序。即所有的行应重新排列。这也适用,但标题和内容表的排列都是徒劳的。
请参阅链接,下面是我的布局xml文件Android-固定标题水平和垂直滚动表
<?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是什么,其中的代码是写在onLayout方法定制的LinearLayout重新调整表的列宽。 代码如下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);
}
}
}
}
如果有的话,请给我提供更好的解决方案。提前致谢。
这是行不通的,因为'ScrollView'只能有一个孩子。 – Metaphore
@Metaphore ya scrollview有oly一个孩子....请再次检查... –
是的,但有什么要点只有一个表的表? – Metaphore