[Activity(Label = "MainGridViewActivity", LaunchMode = LaunchMode.SingleTop)]//设置Activity启动模式
public class MainGridViewActivity : Activity
{
private GridView gview;
JavaList<IDictionary<string, object>> data_list = null;//一定要用javalist不能用IList
IDictionary<string, object> Map = null;
private SimpleAdapter adapter;
// 图片封装为一个数组
private int[] icon = { Resource.Drawable.Icon_agree , Resource.Drawable.icon_graphic,
Resource.Drawable.icon_roll,Resource.Drawable.Icon_semifin, Resource.Drawable.Icon_bg};
private string[] iconName = { "审批", "报表", "成品","半成品", "报工" };
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MainGridView_layout);
gview = FindViewById<GridView>(Resource.Id.gridView1);
//新建List
data_list = new JavaList<IDictionary<string, object>>();//一定要用javalist不能用List
//获取数据
getData();
//新建适配器
string[] from = { "image", "text" };
int[] to = { Resource.Id.imagegViewMain, Resource.Id.textgViewMain };
adapter = new SimpleAdapter(this, data_list, Resource.Layout.gViewMainItem, from, to);
//配置适配器
gview.Adapter = adapter;
//GridView 的itemclick
gview.ItemClick += gview_ItemClick;//给GridView单元格加点击事件,类似java开发中的监听。
}
private JavaList<IDictionary<string, object>> getData()
{
//cion和iconName的长度是相同的,这里任选其一都可以
for (int i = 0; i < icon.Length; i++)
{
Map = new JavaDictionary<string, object>();
Map.Add("image", icon[i]);
Map.Add("text", iconName[i]);
data_list.Add(Map);
}
return data_list;
}
private void gview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
switch (e.Position)
{
case 0://审批请求
{
var intent = new Intent(this, typeof(ApprovalActivity));
StartActivity(intent);
}
break;
case 1:
{
var intent = new Intent(this, typeof(GraphicMain));
StartActivity(intent);
}
break;
case 2:
{
var intent = new Intent(this, typeof(FinishGoodList));
StartActivity(intent);
}
break;
case 3:
{
var intent = new Intent(this, typeof(SemiFinishedGoodsList));
StartActivity(intent);
}
break;
case 4:
{
var intent = new Intent(this, typeof(SubMachineActivity));
StartActivity(intent);
}
break;
default:
new AlertDialog.Builder(this)
.SetMessage("该功能还在开发中...")
.Show();
break;
}
}
}
Xamarin控件使用之GridView
最新推荐文章于 2021-11-27 18:01:16 发布