1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height 7 tools:context="com.hex.demogridview.MainActivity">
8 GridView
9 android:id="@+id/gv_info"
10 android:columnWidth="90dp"
11 android:verticalSpacing="10dp"
12 android:horizontalSpacing13 android:numColumns="auto_fit"
14 android:stretchMode="columnWidth"
15 android:layout_width16 android:layout_height="match_parent"></GridView17 </RelativeLayout>
LinearLayout 3 android:layout_width android:layout_height android:orientation="vertical" 6 ImageView
7 ="@+id/img_info"
8 android:src="@drawable/a011"
9 ="wrap_content"/>
11 TextView
12 ="@+id/tv_info"
android:text="abc"
android:textSize="20sp"
android:textAlignment="center"
android:textColor="@color/colorAccent"
17 18 19 LinearLayout>
class MyAdapter extends BaseAdapter {
2 private Context mContext;
3
4 public MyAdapter(Context context) {
5 this.mContext = context;
6 }
7
8 @Override
9 public int getCount() {
10 return mList != null ? mList.size() : 0;
11 12
13 14 public Object getItem( position) {
15 return mList.get(position);
16 17
18 19 long getItemId(20 position;
21 22
23 24 public View getView( position,View convertView,ViewGroup parent) {
25 ViewHolder viewHolder;
26 if (convertView == null) {
27 // if it's not recycled,initialize some attributes
28 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); getLayoutInflater();
29 convertView = inflater.inflate(R.layout.grid_item,);
30 viewHolder=new ViewHolder();
31 viewHolder.img = (ImageView) convertView.findViewById(R.id.img_info);
32 viewHolder.tv = (TextView) convertView.findViewById(R.id.tv_info);
33
34 convertView.setTag(viewHolder);
35 } else {
36 viewHolder = (ViewHolder) convertView.getTag();
37 }
38 viewHolder.img.setImageResource((int)mList.get(position).get("img"));
39 viewHolder.tv.setText((String) mList.get(position).get("txt"40 convertView;
41 42 }
class MainActivity AppCompatActivity {
2 private [] imgs;
3 private List<Map<String,Object>> mList;
4 GridView gvInfo;
5 @Override
protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
9 gvInfo= (GridView) this.findViewById(R.id.gv_info);
10
11 imgs=new []{R.drawable.a011,R.drawable.a012,R.drawable.a013,R.drawable.a014,R.drawable.a015,R.drawable.a016,
R.drawable.a017,R.drawable.a018,R.drawable.a019,R.drawable.a0110,R.drawable.a0111};
12 mList=new ArrayList<Map<String,1)">();
13 for(int i=0;i<11;i++14 Map<String,Object> map = new HashMap<String,Object>15 map.put("img",imgs[i]);
16 map.put("txt","第" + i + "个" mList.add(map);
19 MyAdapter adapter =new MyAdapter(20 gvInfo.setAdapter(adapter);
21 gvInfo.setOnItemClickListener( AdapterView.OnItemClickListener() {
22 @Override
23 void onItemClick(AdapterView<?> parent,View view,1)">int position,1)">long id) {
24 TextView textView= (TextView) view.findViewById(R.id.tv_info);
25 String msg = textView.getText().toString();
26 Toast.makeText(MainActivity.27 28 });
29
30 }