MainActivity is as follows:
Package CN. c; import Java. util. arraylist; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. gridview; import android. widget. imageview;/*** Requirement Description: * display an image by using a custom gridview adapter ** Note: remove the background color when the gridview is clicked * Android: listselector = "@ and Roid: color/transparent "*/public class mainactivity extends activity {private gridview mgridview; private arraylist <rowimages> marraylist; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {mgridview = (gridview) findviewbyid (R. id. gridview); marraylist = new arraylist <rowimages> (); For (INT I = 0; I <10; I ++) {rowimages images = new rowimages (R. drawable. a, R. drawable. b, R. drawable. a); marraylist. add (images);} gridviewadapter adapter = new gridviewadapter (mainactivity. this, marraylist); mgridview. setadapter (adapter); mgridview. setonitemclicklistener (New itemclicklistenerimpl ();} private class itemclicklistenerimpl implements onitemclicklistener {public void onitemclick (adapterview <?> Parent, view, int position, long ID) {system. out. println ("click the nth" + (Position + 1) + "row"); imageview imageview_left = (imageview) view. findviewbyid (R. id. imageview_left); imageview_left.setonclicklistener (New onclicklistener () {public void onclick (view v) {system. out. println ("xxxxxx click the picture on the left") ;}}); imageview imageview_center = (imageview) view. findviewbyid (R. id. imageview_center); imageview_center.setonclicklistener (New onclicklistener () {public void onclick (view v) {system. out. println ("xxxxxx clicking the picture in the middle") ;}}); imageview imageview_right = (imageview) view. findviewbyid (R. id. imageview_right); imageview_right.setonclicklistener (New onclicklistener () {public void onclick (view v) {system. out. println ("xxxxxx clicking the picture on the right ");}});}}}
GridViewAdapter is as follows:
package cn.c;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;public class GridViewAdapter extends BaseAdapter{ private Context mContext; private ArrayList<RowImages> mArrayList; public GridViewAdapter(Context context,ArrayList<RowImages> arrayList){this.mContext=context;this.mArrayList=arrayList;}public int getCount() {if (mArrayList==null) {return 0;} else {return mArrayList.size();}}public Object getItem(int position) {if (mArrayList==null) {return null;} else { return mArrayList.get(position);}}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder=null;if (convertView==null) {holder=new ViewHolder();convertView=LayoutInflater.from(mContext).inflate(R.layout.gridview_row_layout, null,false);holder.imageView_left=(ImageView) convertView.findViewById(R.id.imageView_left);holder.imageView_center=(ImageView) convertView.findViewById(R.id.imageView_center);holder.imageView_right=(ImageView) convertView.findViewById(R.id.imageView_right);convertView.setTag(holder); } else { holder=(ViewHolder) convertView.getTag(); }if (mArrayList!=null) {RowImages images=this.mArrayList.get(position);if (holder.imageView_left!=null) {holder.imageView_left.setImageResource(images.getImg_left());}if (holder.imageView_center!=null) {holder.imageView_center.setImageResource(images.getImg_center());}if (holder.imageView_right!=null) {holder.imageView_right.setImageResource(images.getImg_right());}}return convertView;}private class ViewHolder{ImageView imageView_left;ImageView imageView_center;ImageView imageView_right;}}
RowImages:
package cn.c;public class RowImages {private int img_left;private int img_center;private int img_right;public RowImages() {super();}public RowImages(int img_left, int img_center, int img_right) {super();this.img_left = img_left;this.img_center = img_center;this.img_right = img_right;}public int getImg_left() {return img_left;}public void setImg_left(int img_left) {this.img_left = img_left;}public int getImg_center() {return img_center;}public void setImg_center(int img_center) {this.img_center = img_center;}public int getImg_right() {return img_right;}public void setImg_right(int img_right) {this.img_right = img_right;}}
Main. xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:verticalSpacing="10dip" android:listSelector="@android:color/transparent" /></RelativeLayout>
Gridview_row_layout.xml is as follows:
<?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="wrap_content" > <ImageView android:id="@+id/imageView_left" android:layout_width="80dip" android:layout_height="80dip" android:src="@drawable/ic_launcher" android:layout_alignParentLeft="true" /> <ImageView android:id="@+id/imageView_center" android:layout_width="80dip" android:layout_height="80dip" android:src="@drawable/ic_launcher" android:layout_centerHorizontal="true" /> <ImageView android:id="@+id/imageView_right" android:layout_width="80dip" android:layout_height="80dip" android:src="@drawable/ic_launcher" android:layout_alignParentRight="true" /> </RelativeLayout>