MainActivity.java
package com.yql.gridviewdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener{
private GridView gridView;
private List<Map<String,Object>> datalist;
private SimpleAdapter adapter;
private int[] drawable= { R.drawable.address_book, R.drawable.calendar,
R.drawable.camera, R.drawable.clock, R.drawable.games_control,
R.drawable.messenger, R.drawable.ringtone, R.drawable.settings,
R.drawable.speech_balloon, R.drawable.weather,
R.drawable.world, R.drawable.youtube };
private String[] iconName = { "联系人", "日历", "照相机", "时钟", "游戏", "短信", "铃声", "设置",
"语音", "天气", "浏览器", "Youtube" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView);
//准备数据源
//新建适配器SimpleAdapter
//GridView加载适配器
//GridView配置事件监听器
datalist = new ArrayList<Map<String,Object>>();
adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"image","iconName"},new int[]{R.id.image,R.id.iconName});
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
private List<Map<String,Object>> getData() {
for(int i=0;i<drawable.length;i++){
Map map = new HashMap<String,Object>();
map.put("image", drawable[i]);
map.put("iconName", iconName[i]);
datalist.add(map);
}
return datalist;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(this, "我是:"+iconName[arg2], Toast.LENGTH_SHORT).show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--android:numColumns="3" 每一行设置几列
android:horizontalSpacing="10dp" 列之间的宽度
android:verticalSpacing="10dp"> 行之间的宽度
-->
<GridView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/gridView"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#000000">
<ImageView
android:src="@drawable/ic_launcher"
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:id="@+id/iconName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="文字"/>
</LinearLayout>