我正在寻找在数据驱动中包含预先构建的数据网格控件,以表格形式显示数据,就像我们在桌面或Web应用程序中一样。我知道有其他方法可供选择,就像我们在这里一样: How to create a DataGrid display in android?
还有其他好的替补吗?
答案 0 :(得分:1)
以下是使用代码的示例布局文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tg="http://schemas.android.com/apk/res/com.ra.music"
xmlns:pc="http://schemas.android.com/apk/res/com.ra.music"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MusicActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/music_header" />
<com.ra.playercontrol.PlayerControl
android:id="@+id/player_control"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
pc:editSong="true" />
<ScrollView
android:id="@+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbars="vertical|horizontal" >
<com.ra.tablegrid.TableGrid
android:id="@+id/music_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:stretchColumns="*" >
<com.ra.tablegrid.TableTextView
android:id="@+id/song_id"
tg:fieldName="song_id"
tg:keyField="true"
tg:visible="false" />
<com.ra.tablegrid.TableImageButton
android:id="@+id/play_song"
android:src="@drawable/play_button_up"
tg:displayOrder="02"
tg:fieldName="play_song"
tg:down_src_table="@drawable/play_button_down" />
<com.ra.tablegrid.TableImageButton
android:id="@+id/edit_song"
android:src="@drawable/edit_button_up"
tg:displayOrder="03"
tg:fieldName="edit_song"
tg:down_src_table="@drawable/edit_button_down" />
<com.ra.tablegrid.TableImageButton
android:id="@+id/delete_song"
android:src="@drawable/delete_button_up"
tg:displayOrder="04"
tg:fieldName="delete_song"
tg:down_src_table="@drawable/delete_button_down" />
<com.ra.tablegrid.TableTextView
android:id="@+id/song_name"
android:textSize="16sp"
tg:clickable="true"
tg:displayOrder="07"
tg:fieldName="song_name"
tg:sortable="true"
tg:fieldPercent="0.26"
tg:title="@string/song_name" />
<com.ra.tablegrid.TableTextView
android:id="@+id/artist"
android:textSize="13sp"
tg:displayOrder="10"
tg:fieldName="artist"
tg:secondarySorts="song_name"
tg:sortable="true"
tg:fieldPercent="0.13"
tg:title="@string/artist" />
<com.ra.tablegrid.TableTextView
android:id="@+id/cd_name"
android:textSize="13sp"
tg:displayOrder="15"
tg:fieldName="cd_name"
tg:secondarySorts="song_name"
tg:sortable="true"
tg:fieldPercent="0.14"
tg:title="@string/cd_name" />
<com.ra.tablegrid.TableTextView
android:id="@+id/category"
android:textSize="13sp"
tg:displayOrder="20"
tg:fieldName="category"
tg:secondarySorts="qualifier1,qualifier2,song_name"
tg:sortable="true"
tg:fieldPercent="0.08"
tg:title="@string/category" />
<com.ra.tablegrid.TableTextView
android:id="@+id/qualifier1"
android:textSize="13sp"
tg:displayOrder="25"
tg:fieldName="qualifier1"
tg:secondarySorts="qualifier2,song_name"
tg:sortable="true"
tg:fieldPercent="0.10"
tg:title="@string/qualifier1" />
<com.ra.tablegrid.TableTextView
android:id="@+id/qualifier2"
android:textSize="13sp"
tg:displayOrder="30"
tg:fieldName="qualifier2"
tg:secondarySorts="song_name"
tg:sortable="true"
tg:fieldPercent="0.14"
tg:title="@string/qualifier2" />
<com.ra.tablegrid.TableTextView
android:id="@+id/length"
android:textSize="13sp"
tg:displayOrder="35"
tg:fieldName="length"
tg:sortable="true"
tg:fieldPercent="0.08"
tg:title="@string/length" />
</com.ra.tablegrid.TableGrid>
</ScrollView>
这是一个用于实现表的代码snipet。
mTableGrid.setInitialSort("song_name", true);
mTableGrid.setOnTableClickListener(new ITableClickListener() {
@Override
public void onTableClick(String columnName, String keyValue) {
MusicActivity.this.onTableClick(columnName, keyValue);
}
});
private void populateTable(ArrayList<DisplaySong> displaySongs) {
mTableGrid.clearData();
for (DisplaySong song : displaySongs) {
mTableGrid.addDataRow(createTableDataRow(song));
}
mTableGrid.buildTable();
}