1. First, let's look at the layout file.
<?xml version="1.0" encoding="utf-8"?><GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="170dp" android:gravity="center" android:horizontalSpacing="30dp" android:numColumns="auto_fit" android:paddingBottom="20dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="20dp" android:stretchMode="columnWidth" android:verticalSpacing="20dp" />
2. inherit a baseadapter to write a pictureadapter
Class pictureadapter extends baseadapter {private layoutinflater Inflater; private list <picture> pictures; // constructor public pictureadapter (string [] titles, int [] images, context) {super (); pictures = new arraylist <picture> (); Inflater = layoutinflater. from (context); For (INT I = 0; I <images. length; I ++) {picture = new picture (Titles [I], images [I]); pictures. add (picture) ;}@overridepublic I NT getcount () {// todo auto-generated method stubif (null! = Pictures) {return pictures. size () ;}else {return 0 ;}@ overridepublic object getitem (INT position) {// todo auto-generated method stubsystem. out. println ("--" + position); Return pictures. get (position) ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubsystem. out. println ("-- 1 ---" + position); Return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stubviewholder viewholder; if (convertview = NULL) {convertview = Inflater. inflate (R. layout. picture_item, null); viewholder = new viewholder (); viewholder. title = (textview) convertview. findviewbyid (R. id. title); viewholder. image = (imageview) convertview. findviewbyid (R. id. image); convertview. settag (viewholder);} else {viewholder = (viewholder) convertview. gettag ();} viewholder. title. settext (pictures. get (position ). gettitle (); viewholder. image. setimageresource (pictures. get (position ). getimageid (); Return convertview;} class viewholder {public textview title; Public imageview image;} class picture {private String title; private int imageid; Public picture () {super () ;}public picture (String title, int imageid) {super (); this. title = title; this. imageid = imageid;} Public String gettitle () {return title;} public void settitle (String title) {This. title = title;} public int getimageid () {return imageid;} public void setimageid () {This. imageid = imageid ;}}
3. R. layout. picture_item layout File
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center" android:padding="4dp" android:scaleType="fitXY" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center_horizontal" android:textColor="#ff501e58" android:textSize="22dip"/></LinearLayout>
4. Variable Declaration
private String[] titles;private GridView gridView;private int[] images;
5. Fill in data and call
The following operations assign values to the titles and images arrays first.
Gridview = (gridview) findviewbyid (R. id. gridview); pictureadapter adapter = new pictureadapter (titles, images, this); gridview. setadapter (adapter); gridview. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Parent, view V, int position, long ID) {// click the corresponding item ...}});