GridView
Layout File Declaration Gridviewjava create GridView Configuration 1, prepare data source 2, new Adapter (Simpleadapter) 3, GridView load Adapter 4, GridView Configuration Listener (Onitemclicklistener) GridView implementation process: 1. Prepare data source datalist=new arraylist<map<string,object>> (); 2. New Adapter (Simpleadapter) adapter=new Simpleadapter (This,getdata (), custom layout, new string[]{"image", "Text"},new int[]{ R.id.image,r.id.text});(GetData method content See note Appendix 1) 3.gridView Load Adapter Gridview.setadapter (adapter) 4.gridView Configuration Time Listener ( Onitemclicklistener) Insert Interface Onitemclicklistenergridview.setonitemclicklistener (this);(Onitemclick see note Appendix 2) 1. GetData method Internal Private list<map<string,object>> GetData () {for (int i=0;i<icon.length;i++) {map<string , Object>map=new hashmap<string,object> (); Map.put ("image", Icon[i]); Map.put ("text", Iconname[i]); Datalist.add (map);} return dataList;} 2.public void Onitemclick (...) {Toast.maketext (this, "I am" +iconname[position],toast.length_short). Show ();
GridView Three properties: android:numcolumns= "" Each row shows how many columns android:horizontalspacing= "" Distance between two columns android:verticalspacing= "" The distance between the two lines in which the android:numcolumns= "Auto_fit" will implement the effect of automatic adaptation, but may actually not look good, or the number of columns should be artificially defined, while modifying the size of the ImageButton, this section of the example is the original WARP_ Content changed to 60DP.
In the manifest file: Android:theme= "@android: Style/theme.black.notitlebar"
1 Public classMainactivityextendsActivityImplementsOnitemclicklistener {2 PrivateGridView GridView;3 PrivateList<map<string, object>>dataList;4 5 PrivateSimpleadapter Adapter;6 int[] icon ={r.drawable.g, r.drawable.h, R.DRAWABLE.K,7 r.drawable.th_qq, R.drawable.a, r.drawable.b, r.drawable.c,8 R.DRAWABLE.D, R.drawable.f, R.drawable.h,};9String[] Iconname = {"Cabbage", "potato", "lotus root Slice", "pumpkin", "tomato", "potato", "bok choy",Ten"Watermelon", "fungus", "cucumber", }; One @Override A protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.fragment_main); thegridview=(GridView) Findviewbyid (r.id.gridview1); - //preparing the data source - //New Adapter - //GridView Load Adapter + //GridView Configuration Event Listener (Onitemclincklistener) -DataList =NewArraylist<map<string, object>>(); + Aadapter =NewSimpleadapter ( This, GetDate (), R.layout.item, at NewString[] {"Image", "Text"},New int[] {r.id.imageview1, - r.id.text}); - Gridview.setadapter (adapter); -Gridview.setonitemclicklistener ( This); - } - in PrivateList<map<string, object>>getDate () { - to + for(inti=0;i<icon.length;i++){ -Map<string, object> map=NewHashmap<string, object>(); theMap.put ("image", Icon[i]); *Map.put ("Text", Iconname[i]); $ datalist.add (map);Panax Notoginseng - } the returndataList; + } A @Override the Public voidOnitemclick (adapterview<?> Parent, view view,intposition, + LongID) { - //TODO auto-generated Method Stub $ $Toast.maketext ( This, "I Am" +iconname[position], 0). Show (); -}
Android Siege Lion GridView (Main menu)