Xamarin 实现android gridview 多选
时间:2014-05-17 15:05:56
收藏:0
阅读:698
参考文章:http://blog.csdn.net/zhouyuanjing/article/details/8372686
GridView初始化代码:
gridViewStudent = FindViewById<GridView> (Resource.Id.gridViewStudent); //GridViewAdapter 自定义的Adapter GridViewAdapter gridAdapter = new GridViewAdapter (this,stu); //更新图片 使用默认SimpleAdapter,需重写ViewBinder //adapter.ViewBinder = new ViewBinder (); gridViewStudent.Adapter = gridAdapter; //gridView.ChoiceMode = ChoiceMode.MultipleModal;//长按多选多选模式 gridViewStudent.ChoiceMode = ChoiceMode.Multiple;//设置多选模式 //GridMultiChoiceListener自定义监听 gridViewStudent.SetMultiChoiceModeListener (new GridMultiChoiceListener(this));// 设置多选模式监听器
GridViewAdapter 实现:
public class GridViewAdapter:BaseAdapter { Context context; List<Student> data; //List<int> selected = new List<int>(); public GridViewAdapter(Context c,List<Student> data){ context = c; this.data = data; } /*public void SelectedItem(int selected) { this.selected.Add (selected); }*/ public override int Count { get { return data.Count; } } public override Java.Lang.Object GetItem (int position) { return position; } public override long GetItemId (int position) { return position; } //主要方法 public override View GetView (int position, View convertView, ViewGroup parent) { GridItem item; if (convertView == null) { item = new GridItem (context); item.LayoutParameters = new GridView.LayoutParams (GridView.LayoutParams.WrapContent, GridView.LayoutParams.WrapContent); } else { item = convertView as GridItem; } item.ID.Text = data[position].ID; item.Name.Text = data [position].Name; return item; } }
GetView 为主要操作方法,用来数据显示等。GridItem为自定义类,标识gridview的一个子视图
GridItem 实现;
public class GridItem:RelativeLayout,ICheckable { bool _checked; public ImageView HeadImg { get; set; } public ImageView SelectedImg { get; set; } public TextView Name { get; set; } public TextView ID { get; set; } public GridItem (Context context) : base (context) { //从布局文件加载元素
LayoutInflater.From(context).Inflate(Resource.Layout.gridItem,this); HeadImg = FindViewById<ImageView>(Resource.Id.imageHead); SelectedImg = FindViewById<ImageView>(Resource.Id.imageSelect); Name = FindViewById<TextView> (Resource.Id.textStuName); ID = FindViewById<TextView> (Resource.Id.textStuID); } //进行状态切换 public bool Checked { get {return _checked; } set { _checked = value; SelectedImg.Visibility=value?ViewStates.Visible:ViewStates.Gone; SetBackgroundDrawable (value ? Resources.GetDrawable (Resource.Drawable.gridBackground) : null); } } public void Toggle () { Checked = !_checked; } }
GridMultiChoiceListener类实现
//这里继承一下Java.Lang.Object对象,不然要实现C#IDisposable接口 class GridMultiChoiceListener:Java.Lang.Object, GridView.IMultiChoiceModeListener { private Context context; public GridMultiChoiceListener(Context context) { this.context = context; } public void OnItemCheckedStateChanged (ActionMode mode, int position, long id, bool isChecked) { //mode.Invalidate (); } public bool OnActionItemClicked (ActionMode mode, IMenuItem item) { return true; } public bool OnCreateActionMode (ActionMode mode, IMenu menu) { return true; } public void OnDestroyActionMode (ActionMode mode) { } public bool OnPrepareActionMode (ActionMode mode, IMenu menu) { return true; } }
评论(0)