效果图:
1.src文件夹下 ShowActivity.java文件内容:
package com.jun.activity;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import com.jun.adapter.MyAdapter;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.GridView;import android.widget.SimpleAdapter;public class ShowActivity extends Activity { private GridView gridView=null; private MyAdapter adapter=null; private SimpleAdapter adapter1=null; private List> list=null; private HashMap map=null; private String data[]={ "图片1","图片2","图片3","图片4","图片5","图6","图片7","图片8","图片9", "图片10","图片11","图片12","图片13","图片14","图片15","图片16"}; private int imgId[]={R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a, R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a, R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); init(); setData(); }private void init(){ gridView=(GridView) findViewById(R.id.grid_view); list=new ArrayList >();}private void setData(){// 使用BaseAdapter添加数据 adapter=new MyAdapter(ShowActivity.this, data, imgId); gridView.setAdapter(adapter); // 使用SimpeAdapter添加数据// for(int i=0;i ();// // map.put("text", data[i]);// map.put("img", imgId[i]);// list.add(map);// // }// adapter1=new SimpleAdapter(ShowActivity.this, list, R.layout.gridview_item, new String[]{"text","img"}, new int[]{R.id.gridview_text,R.id.gridview_img});// gridView.setAdapter(adapter1);} @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_show, menu); return true; }}
2.ShowActivity布局文件activity_show.xml文件内容:
3.继承自BaseAdapter适配器类MyAdapter.java文件内容:
package com.jun.adapter;import com.jun.activity.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseAdapter { private String data[]=null; private int imgId[]=null; private Context context=null; private LayoutInflater inflater=null; public MyAdapter(Context context,String[] data, int[] imgId) { super(); this.data = data; this.imgId = imgId; this.context = context; inflater=LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return data.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } private class Holder{ TextView tv=null; ImageView img=null; public TextView getTv() { return tv; } public void setTv(TextView tv) { this.tv = tv; } public ImageView getImg() { return img; } public void setImg(ImageView img) { this.img = img; } } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub// 获得holder以及holder对象中tv和img对象的实例 Holder holder; if(convertView==null){ convertView=inflater.inflate(R.layout.gridview_item, null); holder=new Holder(); holder.tv=(TextView) convertView.findViewById(R.id.gridview_text); holder.img=(ImageView) convertView.findViewById(R.id.gridview_img); convertView.setTag(holder); }else{ holder=(Holder) convertView.getTag(); }// 为holder中的tv和img设置内容 holder.tv.setText(data[position]); holder.img.setImageResource(imgId[position]);// 注意 默认为返回null,必须得返回convertView视图 return convertView; }}
4.GridView组件上面的布局: gridview_item.xml文件内容: