在手机首页显示的app应用图标都是以网格的形式展示的,所以今天就来学习一下GridView组件。列表分为单列多行和多列多行,如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView。
效果演示:
activity_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:background="#000"
android:orientation="vertical" >
<GridView
android:id="@+id/gview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:numColumns="4"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>
GirdView的一些属性:
android:numColumns="auto_fit" --------列数设置为自动android:columnWidth="90dp",----------每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth"------缩放与列宽大小同步
android:verticalSpacing="10dp"----------垂直边距
android:horizontalSpacing="10dp"-------水平边距
item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="文字"
android:textColor="#ffffff" />
</LinearLayout>
MainActivity.java代码:
package com.glf.gridviewdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private GridView gview;
private List<Map<String, Object>> data_list;
private SimpleAdapter sim_adapter;
// 图片封装为一个数组
private int[] image = { 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[] imageName = { "通讯录1", "日历2", "照相机3", "时钟4", "游戏5", "短信6",
"铃声7", "设置8", "语音9", "天气10", "浏览器11", "视频12" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gview = (GridView) findViewById(R.id.gview); //获得GridView组件
data_list = new ArrayList<Map<String, Object>>(); //新建数组,用来存放图片和文本数据
// 初始化数据
initData();
// 新建适配器,将数据放入适配器中
String[] from = { "image", "text" };
int[] to = { R.id.image, R.id.text };
sim_adapter = new SimpleAdapter(this, data_list, R.layout.item, from,
to);
// 配置适配器,将适配器加载到网格中
gview.setAdapter(sim_adapter);
gview.setOnItemClickListener(new ItemClickListener());
}
/**
* 相应点击事件
*/
class ItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
HashMap<String, Object> item = (HashMap<String, Object>) parent
.getItemAtPosition(position);
Toast.makeText(MainActivity.this, item.get("text").toString(),
Toast.LENGTH_LONG).show();
}
}
/**
* 数据初始化
*/
public List<Map<String, Object>> initData() {
// cion和iconName的长度是相同的,这里任选那个都可以
for (int i = 0; i < image.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", image[i]);
map.put("text", imageName[i]);
data_list.add(map);
}
return data_list;
}
}
源码下载:GridViewDemo源码