ListView 基礎練習
- 使用 Andriod 內建 Layout - SimpleListItem1 來呈現 ListView,Part 3 - Customizing a ListView's Appearance 該篇有內建 layout 的 ListView 執行效果可以看
- ListView 資料來源,要設定 Adapter Property,常見 Adapter 為
- ArrayAdapter - 該練習是使用它
- SimpleAdapter - SimpleListItem2 會用到
- BaseAdapter - 自訂 ListView 用
- CursorAdapter - 搭配 SQLLite 會用上
- 點擊 ListView 上的 Item 會觸發 ItemClick 事件
Main.axml:就放一個 ListView 控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</LinearLayout>
MainActivity.cs
namespace ListViewBase
{
[Activity(Label = "ListViewBase", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
List<string> data = new List<string>();
data.Add("Xamarin");
data.Add("SQL Server 2016");
data.Add("Windows Server 2016");
data.Add("Visual Studio 2015");
data.Add("Visual Studio Code");
data.Add("Azure");
ArrayAdapter<string> source = new ArrayAdapter<string>(
this,
Android.Resource.Layout.SimpleListItem1,
data);
ListView list = FindViewById<ListView>(Resource.Id.listView1);
list.Adapter = source;
list.ItemClick += (sender, e) =>
{
Toast.MakeText(this, data[e.Position], ToastLength.Short).Show();
};
}
}
}
沒有留言:
張貼留言