1、先来看下布局文件
<?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、继承一个BaseAdapter写一个PictureAdapter
class PictureAdapter extends BaseAdapter { private LayoutInflater inflater; private List<Picture> pictures; //构造函数 public PictureAdapter(String[] titles, int[] images, Context context) { super(); pictures = new ArrayList<Picture>(); inflater = LayoutInflater.from(context); for (int i = 0; i < images.length; i++) { Picture picture = new Picture(titles[i], images[i]); pictures.add(picture); } } @Override public int getCount() { // TODO Auto-generated method stub if (null != pictures) { return pictures.size(); } else { return 0; } } @Override public Object getItem(int position) { // TODO Auto-generated method stub System.out.println("--" + position); return pictures.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub System.out.println("--1---" + position); return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder 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; } }
<?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、变量声明
private String[] titles; private GridView gridView; private int[] images;
其中下面的执行是先为titles 与images数组赋值,这里不再给出
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) { //点击对应Item时的相应... } });