標籤:
ListView 控制項是一個條目容器, 用於顯示集合對象(如數組, List<T>, ObservableCollection<T>等)的每一個條目, 並提供滾動功能.
列表視圖是UI, 集合對象是資料, 兩者肯定不能直接相關聯, 必須藉助一個轉換器來實現綁定, 安卓系統中的這個轉換器被稱為 Adapter (適配器).
安卓提供了一些預定義的適配器以供使用, 如ArrayAdapter(數組適配器), 這些類都繼承於 BaseAdapter ,能滿足一般需求, 不過大部分情況下需要定義自己的適配器以實現更多功能. 具體實現一般分為以下幾步:
1. 定義儲存資料的實體物件. 例: 定義一個實體類 TestEntity
public class TestEntity { public int ID { get; set; } public string Name { get; set; } }
2. 設計資料範本, 此模板會被應用於每一個條目項. 例: 在 Resources/layout/ 檔案夾下建立一個布局檔案, 命名為 TestEntityDataTemplate.axml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/TestEntityID" /> <TextView android:text="Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="5" android:id="@+id/TestEntityName" /></LinearLayout>
3. 定製適配器類.
例: 定義一個類 TestAdapter 繼承於 BaseAdapter<T> 泛型類, 並實現四個抽象方法, ① 索引器(在非泛型類BaseAdapter中為GetItem方法). ② Count屬性. ③ GetItemId方法. ④ GetView方法. 其中, GetView方法返回一個視圖對象, 此方法會在ListView要顯示一個條目項時被調用, 此方法所返回的視圖對象就是這個條目項的視圖
自訂的適配器類一般需要三個對象, 內容物件, 資源id值, 條目對象
class TestAdapter : BaseAdapter<TestEntity> { private Context context; private int resourceId; private List<TestEntity> items = null; public TestAdapter(Context context, int resourceId, List<TestEntity> list) { this.context = context; this.resourceId = resourceId; this.items = list; } public override TestEntity this[int position] { get { return this.items[position]; } } public override int Count { get { return this.items.Count; } } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { TestEntity item = items[position]; //如果沒有獲得視圖才建立新視圖, 提升運行效率 if (convertView == null) { //執行個體化布局 convertView = LayoutInflater.From(this.context).Inflate(this.resourceId, null); } convertView.FindViewById<TextView>(Resource.Id.TestEntityID).Text = item.ID.ToString(); convertView.FindViewById<TextView>(Resource.Id.TestEntityName).Text = item.Name; return convertView; } }
4. 至此, 已經定義好了資料類, 資料範本和適配器類, 接下來只需建立相應的對象, 並將 ListView 綁定到適配器就行了.
例: 建立資料集合與配接器物件, 聲明為類的欄位,以方便在其它方法中使用
private List<TestEntity> testEntityCollection = new List<TestEntity>(); private TestAdapter testAdapter = null;
設定 ListView 的 Adapter 屬性即可完成綁定
ListView listView1 = FindViewById<ListView>(Resource.Id.listView1); testAdapter = new TestAdapter(this, Resource.Layout.TestEntityDataTemplate, testEntityCollection); listView1.Adapter = testAdapter;
當資料集合發生變化時, 記得調用適配器的 NotifyDataSetChanged() 方法通知 UI 進行更新
private void BtnAdd_Click(object sender, EventArgs e) { int id = testEntityCollection.Count + 1; testEntityCollection.Add(new TestEntity() { ID = id, Name = "test-" + id.ToString() }); // testAdapter.NotifyDataSetChanged(); }
Android--ListView與資料繫結(Xamarin)