//1,准备数据源
//2,新建适配器//4.GridView配置事件监听器(OnItemClickListener)
GridView三个重要属性:
android:numColumns="" 每一行显示多少列
android:horizontalSpacing="" 两列之间的间距
android:verticalSpacing="" 两行之间的间距
下面请看代码示例:
1.两个布局
>1 GridView空间布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
android:numColumns="" 每一行显示多少列
android:horizontalSpacing="" 两列之间的间距
android:verticalSpacing="" 两行之间的间距
-->
<GridView
android:layout_marginTop="10dp"
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"/>
</RelativeLayout>
>2 Item样式布局
<?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="#000">
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:text="icon"/>
</LinearLayout>
一个java类:
public class MainActivity extends Activity implements OnItemClickListener{
private GridView mGridView;
private List<Map<String,Object>> dataList;
private int[] icon = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
private String[] iconName = {"图标1","图标2","图标3","图标4","图标5","图标6","图标7","图标8","图标9","图标10","图标11","图标12"};
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.gridView);
//1,准备数据源
//2,新建适配器
//3.GridView加载适配器
//4.GridView配置事件监听器(OnItemClickListener)
dataList = new ArrayList<Map<String,Object>>();
adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"image","name"}, new int[]{R.id.img,R.id.name});
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(this);
}
private List<? extends Map<String, ?>> getData() {
for (int i = 0; i < icon.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", icon[i]);
map.put("name", iconName[i]);
dataList.add(map);
}
return dataList;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, "您点击了" + iconName[arg2], Toast.LENGTH_LONG).show();
}
}
源码下载地址:http://download.csdn.net/detail/weimo1234/8425355
原文:http://blog.csdn.net/weimo1234/article/details/43525555