1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context="com.hanqi.testapp2.TestActivity10">
11
12 <GridView
13 android:layout_width="match_parent"
14 android:layout_height="match_parent"
15 android:id="@+id/gv_1"
16 android:numColumns="4"
17 android:stretchMode="columnWidth"
18 android:gravity="center"
19 android:horizontalSpacing="30dp"></GridView>
20
21 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent">
6
7 <ImageView
8 android:layout_width="110dp"
9 android:layout_height="110dp"
10 android:id="@+id/iv_3"/>
11
12 <TextView
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:id="@+id/tv_9"
16 android:text="介绍"/>
17
18 </LinearLayout>
1 package com.hanqi.testapp2;
2
3 import android.os.Bundle;
4 import android.support.v7.app.AppCompatActivity;
5 import android.util.Log;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.widget.BaseAdapter;
9 import android.widget.GridView;
10 import android.widget.ImageView;
11 import android.widget.TextView;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 public class TestActivity10 extends AppCompatActivity {
17
18 GridView gv_1;
19
20 //List<Integer>liv;
21 List<MyClass>lm;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_test10);
27
28 GridView gv_1=(GridView)findViewById(R.id.gv_1);
29
30 lm=new ArrayList<>();
31
32 MyClass myClass=new MyClass(R.drawable.f1,"美食1");
33
34 lm.add(myClass);
35
36 //1.得到数据 图片的id
37 // liv=new ArrayList<>();
38 // liv.add(R.drawable.f1);
39 // liv.add(R.drawable.f2);
40 // liv.add(R.drawable.f3);
41 // liv.add(R.drawable.f4);
42 // liv.add(R.drawable.f5);
43 // liv.add(R.drawable.f6);
44 // liv.add(R.drawable.f7);
45 // liv.add(R.drawable.f8);
46 lm.add(new MyClass(R.drawable.f2,"美食2"));
47 lm.add(new MyClass(R.drawable.f2,"美食3"));
48 lm.add(new MyClass(R.drawable.f2,"美食4"));
49 lm.add(new MyClass(R.drawable.f2,"美食5"));
50 lm.add(new MyClass(R.drawable.f2,"美食6"));
51 lm.add(new MyClass(R.drawable.f2,"美食7"));
52 lm.add(new MyClass(R.drawable.f2,"美食8"));
53
54 GridAdapter gridAdapter=new GridAdapter();
55
56 gv_1.setAdapter(gridAdapter);
57
58
59 //gv_1.setAdapter(new ImageAdapter());
60 }
61
62 //适配器
63 // class ImageAdapter extends BaseAdapter
64 // {
65 // @Override
66 // public int getCount() {
67 // return liv.size();
68 // }
69 //
70 // @Override
71 // public Object getItem(int position) {
72 // return liv.get(position);
73 // }
74 //
75 // @Override
76 // public long getItemId(int position) {
77 // return 0;
78 // }
79 //
80 // @Override
81 // public View getView(int position, View convertView, ViewGroup parent) {
82 //
83 // //1.得到数据 图片的id
84 // Integer ivid=liv.get(position);
85 //
86 // //2.准备视图View
87 // if (convertView==null) {
88 // convertView = new ImageView(TestActivity10.this);
89 // }
90 //
91 // //3.适配
92 // //转换
93 // ImageView imageView=(ImageView)convertView;
94 //
95 // imageView.setImageResource(ivid);
96 //
97 //// imageView.setMaxWidth(70);
98 //// imageView.setMaxHeight(70);
99 //
100 // imageView.setLayoutParams(new GridView.LayoutParams(110,110));
101 //
102 // return imageView;
103 // }
104 // }
105
106 class MyClass {
107 //img
108 private int img;
109
110 //name
111 private String name;
112
113 public int getImg() {
114 return img;
115 }
116
117 public void setImg(int img) {
118 this.img = img;
119 }
120
121 public String getName() {
122 return name;
123 }
124
125 public void setName(String name) {
126 this.name = name;
127 }
128
129 //直接初始化属性的构造方法
130 public MyClass(int img,String name)
131 {
132 this.img=img;
133 this.name=name;
134 }
135 }
136
137 class GridAdapter extends BaseAdapter
138 {
139 @Override
140 public int getCount() {
141 return lm.size();
142 }
143
144 @Override
145 public Object getItem(int position) {
146 return lm.get(position);
147 }
148
149 @Override
150 public long getItemId(int position) {
151 return 0;
152 }
153
154 @Override
155 public View getView(int position, View convertView, ViewGroup parent) {
156
157
158
159 MyClass myClass=lm.get(position);
160
161 if (convertView==null)
162 {
163 Log.e("TAG", "适配器 getView" + position);
164
165 convertView=View.inflate(TestActivity10.this,R.layout.listview_layout,null);
166 }
167
168 ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_3);
169 imageView.setImageResource(myClass.getImg());
170
171 TextView textView=(TextView)convertView.findViewById(R.id.tv_9);
172 textView.setText(myClass.getName());
173
174 return convertView;
175 }
176 }
177 }