Objective
This blog describes the development of the GridView control under the Android platform. Some common properties, methods, and considerations for the GridView control are explained, and the use of the GridView control is shown in a demo.
Gridview
GridView a control that presents data in a two-dimensional grid format, which has the same inheritance structure as the ListView, inherits directly from the Abslistview, and binds the data with an adapter for the ListAdapter interface. In fact, its display is somewhat similar to the tablelayout layout, you can look at another blog: android--ui layout. People who believe in UI programming experience are not unfamiliar with the GridView control.
Gridview:
For the GridView, there are some specific properties that need to be understood, and Android also provides a corresponding Getter/setter method for these properties:
- Android:columnwidth: Specifies the fixed width of each column.
- Android:gravity: Specifies the fill direction for each fill item.
- Android:horizontalspacing: Specifies the spacing between horizontal rows.
- Android:verticalspacing: Specifies the spacing between vertical columns.
- Android:numcolumns: Specifies the number of columns.
For the Android:numcolumns property, if set to 1, it can be used as a ListView. The GridView method and events, most of which are similar to the ListView, can be found in another blog that explains the ListView, which is no longer detailed here.
Data population
The GridView, like the ListView, needs to populate the data with a ListAdapter adapter. For complex data, it is common to choose to inherit Baseadapter. For some simple data styles, shown through XML layouts, you can use Simpleadapter. They are the more commonly used listadapter subclasses.
The following is a brief demo of the use of the GridView.
Layout code:
1 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" 2 xmlns:tools= "/HTTP/ Schemas.android.com/tools "3 android:layout_width=" match_parent "4 android:layout_height=" match_parent "5 Android oid:orientation= "vertical" 6 android:paddingbottom= "@dimen/activity_vertical_margin" 7 android:paddingleft= "@dimen /activity_horizontal_margin "8 android:paddingright=" @dimen/activity_horizontal_margin "9 android:paddingTop=" @dim En/activity_vertical_margin "Ten tools:context=". Mainactivity ">11 <gridview13 android:id=" @+id/gridview [android:layout_width=] Match_parent "Android:layout_height=" wrap_content "android:horizontalspacing=" 6DP "3 android:numcolumns=" "Android:padding=" 20DP "android:verticalspacing=" 6DP "/>20 <imageview22 Android:id= "@+id/iamgeview" android:layout_width= "match_parent" android:layout_height= "150DP"/>25 </LinearLayout>
Implementation code:
1 package Com.bgxt.gridviewdemo; 2 3 Import java.util.ArrayList; 4 Import Java.util.HashMap; 5 Import Java.util.List; 6 Import Java.util.Map; 7 8 Import Android.os.Bundle; 9 Import android.app.activity;10 Import android.view.menu;11 import android.view.view;12 Import ANDROID.WIDGET.ADAPTERVIEW;13 Import android.widget.gridview;14 Import android.widget.imageview;15 Import ANDROID.WIDGET.SIMPLEADAPTER;16 Import android.widget.adapterview.onitemclicklistener;17 Import ANDROID.WIDGET.ADAPTERVIEW.ONITEMSELECTEDLISTENER;18 public class Mainactivity extends Activity {+ Private Imag Eview imageview;22 Private int[] Resids = new int[] {r.drawable.bmp1, r.drawable.bmp2,23 R.drawable.bmp3, R.drawable.bmp4, R.DRAWABLE.BMP5, r.drawable.bmp6,24 R.DRAWABLE.BMP7, R.DRAWABLE.BMP8, R.DRAWABLE.BMP9, R.dra WABLE.BMP10};25 @Override27 protected void onCreate (Bundle savedinstancestate) {super.oncreate (save Dinstancestate); SetcontentviEW (R.layout.activity_main); GridView GridView = (GridView) Findviewbyid (r.id.gridview); ImageView = (I Mageview) Findviewbyid (R.id.iamgeview); list<map<string, object>> List = new Arraylist<map<str ING, object>> (); (int i = 0; i < resids.length; i++) {map<string, object> cell = new hashmap<string, object> (); Cell.put ("ImageView", Resids[i]); List.add (cell); 37 }38 simpleadapter simpleadapter = new Simpleadapter (mainactivity.this,40 list, r.layout.c ell, new string[] {"ImageView"},41 new int[] {R.id.ivcell}); Gridview.setadapter (simpleadapt ER); Gridview.setonitemclicklistener (ItemClick); Imageview.setimageresource (Resids[0]); 45}46 47 @Override48 public boolean Oncreateoptionsmenu (Menu menu) {Inflate the menu, this adds items to the A Ction Bar if it is Present.50 getmenuinflater (). Inflate (R.menu.main, menu); return true;52}53 the private Onitemclicklis Tener ItemClick = new Onitemclicklistener () {@Override57 public void Onitemclick (Adapterview<?> ; Parent, view view, int position,58 long ID) {//TODO auto-generated method stub60 Imageview.setimageresource (Resids[position]); 61}62};63 64}
Effect Show:
SOURCE download
Please support the original, respect for the original, reproduced please indicate the source. Thank you.
Android--ui's GridView