Xamarin控件使用之GridView
1 [Activity(Label = "MainGridViewActivity", LaunchMode = LaunchMode.SingleTop)]//设置Activity启动模式
2 public class MainGridViewActivity : Activity
3 {
4 private GridView gview;
5 JavaList<IDictionary<string, object>> data_list = null;//一定要用javalist不能用IList
6 IDictionary<string, object> Map = null;
7 private SimpleAdapter adapter;
8 // 图片封装为一个数组
9 private int[] icon = { Resource.Drawable.Icon_agree , Resource.Drawable.icon_graphic,
10 Resource.Drawable.icon_roll,Resource.Drawable.Icon_semifin, Resource.Drawable.Icon_bg};
11 private string[] iconName = { "审批", "报表", "成品","半成品", "报工" };
12 protected override void OnCreate(Bundle savedInstanceState)
13 {
14 base.OnCreate(savedInstanceState);
15 SetContentView(Resource.Layout.MainGridView_layout);
16
17 gview = FindViewById<GridView>(Resource.Id.gridView1);
18 //新建List
19 data_list = new JavaList<IDictionary<string, object>>();//一定要用javalist不能用List
20 //获取数据
21 getData();
22 //新建适配器
23 string[] from = { "image", "text" };
24 int[] to = { Resource.Id.imagegViewMain, Resource.Id.textgViewMain };
25 adapter = new SimpleAdapter(this, data_list, Resource.Layout.gViewMainItem, from, to);
26 //配置适配器
27 gview.Adapter = adapter;
28
29 //GridView 的itemclick
30 gview.ItemClick += gview_ItemClick;//给GridView单元格加点击事件,类似java开发中的监听。
31 }
32 private JavaList<IDictionary<string, object>> getData()
33 {
34 //cion和iconName的长度是相同的,这里任选其一都可以
35 for (int i = 0; i < icon.Length; i++)
36 {
37 Map = new JavaDictionary<string, object>();
38 Map.Add("image", icon[i]);
39 Map.Add("text", iconName[i]);
40 data_list.Add(Map);
41 }
42 return data_list;
43 }
44 private void gview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
45 {
46 switch (e.Position)
47 {
48 case 0://审批请求
49 {
50 var intent = new Intent(this, typeof(ApprovalActivity));
51 StartActivity(intent);
52 }
53 break;
54 case 1:
55 {
56 var intent = new Intent(this, typeof(GraphicMain));
57 StartActivity(intent);
58 }
59 break;
60 case 2:
61 {
62 var intent = new Intent(this, typeof(FinishGoodList));
63 StartActivity(intent);
64 }
65 break;
66 case 3:
67 {
68 var intent = new Intent(this, typeof(SemiFinishedGoodsList));
69 StartActivity(intent);
70 }
71 break;
72 case 4:
73 {
74 var intent = new Intent(this, typeof(SubMachineActivity));
75 StartActivity(intent);
76 }
77 break;
78 default:
79 new AlertDialog.Builder(this)
80 .SetMessage("该功能还在开发中...")
81 .Show();
82 break;
83 }
84 }
85 }