Android--ListView与数据绑定(Xamarin)

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();
        }