Xamarin implements Android GridView multi-Select

Source: Internet
Author: User

Reference article: http://blog.csdn.net/zhouyuanjing/article/details/8372686

GridView Initialization code:

Gridviewstudent = findviewbyid<gridview>(Resource.Id.gridViewStudent);//Gridviewadapter Custom AdapterGridviewadapter Gridadapter =NewGridviewadapter ( This, Stu); //update picture Use default Simpleadapter, need to rewrite Viewbinder//adapter. Viewbinder = new Viewbinder ();Gridviewstudent.adapter =Gridadapter; //gridview.choicemode = choicemode.multiplemodal;//Long Press Multi-select multi-select modeGridviewstudent.choicemode = Choicemode.multiple;//set Multi-select mode//Gridmultichoicelistener Custom MonitoringGridviewstudent.setmultichoicemodelistener (NewGridmultichoicelistener ( This));//setting up a multi-select mode listener
Gridviewadapter implementation:
 Public classGridviewadapter:baseadapter {context context; List<Student>data; //list<int> selected = new list<int> ();         PublicGridviewadapter (Context c,list<student>data) {Context=C;  This. data =data; }        /*Public void SelectedItem (int selected) {this.selected.Add (selected); }*/         Public Override intCount {Get {                returndata.            Count; }        }         Public OverrideJava.Lang.Object GetItem (intposition) {            returnposition; }         Public Override LongGetitemid (intposition) {            returnposition; }                                //Main Methods         Public OverrideView GetView (intposition, View Convertview, ViewGroup parent)            {GridItem item; if(Convertview = =NULL) {Item=NewGridItem (context); Item. Layoutparameters=Newgridview.layoutparams (GridView.LayoutParams.WrapContent, GridView.LayoutParams.WrapContent); } Else{Item= Convertview asGridItem; } item.id. Text=data[position].id; Item. Name.text=data [position].            Name; returnitem; }    }

GetView is the main operation method, used for data display and so on. GridItem as a custom class that identifies a child view of the GridView

GridItem implementation;

 Public classgriditem:relativelayout,icheckable {BOOL_checked;  PublicImageView headimg {Get; Set; }         PublicImageView selectedimg {Get; Set; }         PublicTextView Name {Get; Set; }         PublicTextView ID {Get; Set; }         PublicGridItem (Context context):Base(context) {//loading elements from a layout file
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); } //make a state switch Public BOOLChecked {Get{return_checked;} Set{_checked=value; Selectedimg.visibility=value?ViewStates.Visible:ViewStates.Gone; Setbackgrounddrawable (Value? Resources.getdrawable (Resource.Drawable.gridBackground):NULL); } } Public voidToggle () {Checked= !_checked; } }
Gridmultichoicelistener class implementation
//inherit the Java.Lang.Object object here, or implement the C#idisposable interfaceclassGridMultiChoiceListener:Java.Lang.Object, Gridview.imultichoicemodelistener {Privatecontext Context;  PublicGridmultichoicelistener (Context context) { This. Context =context; }         Public voidOnitemcheckedstatechanged (Actionmode mode,intPositionLongIdBOOLisChecked) {            //mode. Invalidate ();        }         Public BOOLonactionitemclicked (actionmode mode, Imenuitem item) {return true; }         Public BOOLOncreateactionmode (actionmode mode, Imenu menu) {return true; }         Public voidOndestroyactionmode (Actionmode mode) {} Public BOOLOnprepareactionmode (actionmode mode, Imenu menu) {return true; }                }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.