mainActivity如下:
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; /** * 需求描述: * 自定义GridView的Adapter展示图片 * * 注意:去掉GridView点击时的背景色 * android:listSelector="@android: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 view, int position,long id) { System.out.println("点击了第"+(position+1)+"行"); ImageView imageView_left=(ImageView) view.findViewById(R.id.imageView_left); imageView_left.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("xxxxxx 点击了左边的图片"); } }); ImageView imageView_center=(ImageView) view.findViewById(R.id.imageView_center); imageView_center.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("xxxxxx 点击了中间的图片"); } }); ImageView imageView_right=(ImageView) view.findViewById(R.id.imageView_right); imageView_right.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("xxxxxx 点击了右边的图片"); } }); } } }
GridViewAdapter如下:
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如下:
<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如下:
<?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>