グリッドビューである必要がありますか?ListViewは機能しますか?
各行に2つのアイテムを表示するListViewとListActivityを作成しました。SDKが提供するsimple_list_item_2.xmlレイアウトから始めました。このレイアウトでは、行ごとに2つのアイテムが一覧表示されますが、小さいフォントを使用して2番目の行を重ねて(2行)配置します。私が欲しかったのは、同じ行にある両方のアイテムで、1つは右に、もう1つは左にありました。
まず、simple_list_item_2.xmlをプロジェクトのres / layoutディレクトリに新しい名前でコピーし、ビュー要素名を「TwoLineListItem」のままにして、プロパティandroid:mode="twoLine"を"oneLine"に変更しました。次に、2つの内側の要素を、自分が望むことを実行する要素に置き換えました。
リストを初期化するコードで、MatrixCursorを作成し、目的のデータを入力しました。2つのアイテムをサポートするには、MatrixCursorの各行に3つの列が必要です。1つは主キー「_id」で、他の2つの列は表示したいアイテムです。その後、SimpleCursorAdapterを使用して、ListViewに入力して制御することができました。
私のレイアウトXMLファイル:
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="oneLine"
>
<TextView
android:id="@android:id/text1"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@android:id/text2"
android:gravity="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:layout_marginRight="6dip"
android:layout_toRightOf="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/MainFontColor"
/>
</TwoLineListItem>
「左」と「右」のandroid:gravity値を使用して、左側のアイテムを左揃えにし、右側のアイテムを右揃えにしていることに注意してください。レイアウトに異なる重力値が必要になります。さらに、私が必要としなかった左側のアイテムのサイズを制御するためのプロパティが必要になります。
ListViewを初期化したListActivityクラスのメソッド:
private void initListView()
{
final String AuthorName = "Author: ";
final String CopyrightName = "CopyRight: ";
final String PriceName = "Price: ";
final String[] matrix = { "_id", "name", "value" };
final String[] columns = { "name", "value" };
final int[] layouts = { android.R.id.text1, android.R.id.text2 };
MatrixCursor cursor = new MatrixCursor(matrix);
DecimalFormat formatter = new DecimalFormat("##,##0.00");
cursor.addRow(new Object[] { key++, AuthorName, mAuthor });
cursor.addRow(new Object[] { key++, CopyrightName, mCopyright });
cursor.addRow(new Object[] { key++, PriceName,
"$" + formatter.format(mPrice) });
SimpleCursorAdapter data =
new SimpleCursorAdapter(this,
R.layout.viewlist_two_items,
cursor,
columns,
layouts);
setListAdapter( data );
} // end of initListView()
MatrixCursorコンストラクターのパラメーターは、カーソル内の列の順序と名前を定義する文字列の配列です。重要:必ず「_id」列を追加してください。追加しないと、MatrixColumnが例外をスローし、機能しなくなります。
3つの変数、mAuthor、mCopyright、およびmPriceは、私のListAdaptorクラスの3つのデータメンバーであり、他の場所で初期化されます。私の実際のコードでは、mAuthorは実際には、作成者名のリストからこのメソッドに組み込まれています。著者名は、名前間の区切り文字として「\ n」を使用して、単一の文字列に連結されます。これにより、複数の作成者名が同じTextViewの異なる行に表示されます。
SimpleCursorAdapter ctorパラメーターは、各リスト行に使用するビューのID、データを含むカーソル、各要素がカーソルからの列の名前(取得順に)である文字列の配列です。リスト行の各アイテムに使用するビューのビューIDの配列。