The main implementation to the GridView plus checkbox selected after the mask can be added Fruitadapter
Adding a checkbox and mask is just a matter of finishing in Fruitadapter
Public class fruitadapter extends baseadapter { PrivateLayoutinflater Minflater;PrivateList<fruit> mfruits;Private BooleanMcheckboxmanager[]; Public Fruitadapter(Layoutinflater minflater, list<fruit> mfruits) { This. Minflater = Minflater; This. mfruits = mfruits; Mcheckboxmanager =New Boolean[Mfruits.size ()]; }@Override Public int GetCount() {returnMfruits.size (); }@Override PublicObjectGetItem(intPosition) {returnPosition }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(Final intPosition, View Convertview, ViewGroup parent) {Viewholder VH =NULL;if(Convertview = =NULL) {Convertview = Minflater.inflate (R.layout.gridview_item,NULL); VH =NewViewholder (); Vh.imageview = (ImageView) Convertview.findviewbyid (r.id.image); Vh.textview = (TextView) Convertview.findviewbyid (R.id.textview); Vh.checkbox = (CheckBox) Convertview.findviewbyid (R.id.checkbox); Vh.imageviewtint = (ImageView) Convertview.findviewbyid (r.id.imageview_tint); Convertview.settag (VH); }Else{VH = (Viewholder) convertview.gettag (); } Fruit Fruit = Mfruits.get (position); Vh.imageView.setImageResource (Fruit.getimg ()); Vh.textView.setText (Fruit.getname ()); Vh.checkBox.setOnCheckedChangeListener (NewCompoundbutton.oncheckedchangelistener () {@Override Public void oncheckedchanged(Compoundbutton Buttonview,BooleanisChecked) {mcheckboxmanager[position] = isChecked; LOG.D ("Masks",""+ mcheckboxmanager[position] + position); Notifydatasetchanged (); } }); Vh.checkBox.setChecked (Mcheckboxmanager[position]);if(Mcheckboxmanager[position]) {vh.imageViewTint.setVisibility (view.visible); }Else{vh.imageViewTint.setVisibility (view.invisible); }returnConvertview; }StaticClass Viewholder {TextView TextView; ImageView ImageView; CheckBox checkbox; ImageView Imageviewtint; }}
In addition, the following XML file implements the mask
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" match_parent " android:layout_height =" match_parent " android:orientation =; <imageview android:id
= "@+id/image" android:layout_width =< Span class= "Hljs-value" > "100DP" android:layout_height =" 100DP " android:layout_centerinparent =" true "/> <textview android:id = "@+id/textview" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@+id/image" android:layout_centerhorizontal = android:layout_margintop =< Span class= "Hljs-value" > "10DP" android:gravity =" center "/> <CheckBoxandroid:id= "@+id/checkbox"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:layout_alignparentright= "true" android:focusable="false" /> <!--This imageview when the key to implement the mask needs to set visibility start to invisible; use tint to set the background color mask do not know why is not implemented, and then changed to background, Also have the classmate said should use AlignTop and alignleft, I tried all can, not this reason, the current elimination method the only difference is to use background tint above ... - <ImageViewandroid:id="@+id/imageview_tint"android:layout_width= "Match_parent"android:layout_height="Match_parent"android:background="@ Color/green_tind "android:visibility=" Invisible "android:layout_alignright ="@+id/checkbox"android:layout_alignbottom="@+id/textview" /> </relativelayout>
The following are other files
Public class gridviewactivity extends actionbaractivity { PrivateLayoutinflater Minflater;Privatelist<fruit>mfruits;PrivateGridView Mgridview;PrivateFruitadapter Fruitadapter;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_grid_view); mgridview= (GridView) Findviewbyid (R.id.gridview);//Forget to get Inflater will report null pointerMinflater=getlayoutinflater (); mfruits=NewArraylist<> (); for(intI=0;i<Ten; i++) {Fruit apple=NewFruit ("Apple", R.MIPMAP.CAR1); Fruit banana=NewFruit ("Banana", R.MIPMAP.CAR2); Fruit grape=NewFruit ("Grape", R.MIPMAP.CAR3); Fruit car=NewFruit ("Grape", R.MIPMAP.CAR4); Fruit car1=NewFruit ("Grape", R.MIPMAP.CAR5); Fruit car2=NewFruit ("Grape", R.MIPMAP.CAR6); Fruit car3=NewFruit ("Grape", R.MIPMAP.CAR7); Fruit car4=NewFruit ("Grape", R.MIPMAP.CAR8); Fruit car5=NewFruit ("Grape", R.MIPMAP.CAR9); Mfruits.add (Apple); Mfruits.add (banana); Mfruits.add (grape); Mfruits.add (car); Mfruits.add (CAR1); Mfruits.add (CAR2); Mfruits.add (CAR3); Mfruits.add (CAR4); Mfruits.add (CAR5); } fruitadapter=NewFruitadapter (minflater,mfruits); Mgridview.setadapter (Fruitadapter); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {Getmenuinflater (). Inflate (R.menu.menu_grid_view, menu);return true; }@Override Public Boolean onoptionsitemselected(MenuItem Item) {intid = item.getitemid ();if(id = = r.id.action_settings) {return true; }return Super. onoptionsitemselected (item); }}******************************************** Public class Fruit { PrivateString name;Private intimg Public Fruit(String name,intIMG) { This. name = name; This. img = img; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int getimg() {returnimg } Public void setimg(intIMG) { This. img = img; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Imitation qq to send pictures when added mask (want to see sports car Please enter)