Android笔记(二十二) Android中的GridView

时间:2022-12-01 12:52:45

GridView和ListView一样,是Android中比较常见的布局控件,譬如我们的手机桌面其实就是一个GridView。

效果:

Android笔记(二十二) Android中的GridView

实现过程和ListView类似,也是通过Adapter将数据源展示在GridView中去,代码去下:

MainActivity.java

package cn.lixyz.gridviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { private GridView gridView;
private List<Map<String, Object>> dataList;
private int[] icon = {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 = {"通讯录", "日历", "照相机", "时钟", "游戏", "短信",
"铃声", "设置", "语音", "天气", "浏览器", "视频"};
private SimpleAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView); //1. 准备数据源
//2. 新建适配器SimpleAdapter
//3. GridView加载适配器
//4. GridView配置监听器
dataList = new ArrayList<Map<String, Object>>();
adapter = new SimpleAdapter(this, getData(), R.layout.item,
new String[]{"image", "text"}, new int[]{R.id.image, R.id.text}); gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this); } private List<Map<String, Object>> getData() { for (int i = 0; i < icon.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", icon[i]);
map.put("text", iconName[i]);
dataList.add(map);
} return dataList;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "我是" + iconName[position], Toast.LENGTH_SHORT).show(); }
}

activity_main.xml

<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"> <!--
android:numColumns 每一行显示多少列
android:horizontalSpacing 两列之间的间距
android:verticalSpacing 两行之间的间距
--> <GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:verticalSpacing="10dp" /> </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:gravity="center"
android:orientation="vertical"> <ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
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="#000000" /> </LinearLayout>

GridView还有共有以下属性可供开发时使用

属性名称

描述

android:columnWidth

设置列的宽度。关联的方法为:setColumnWidth(int)

android:gravity

设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical 可以多选,用“|”分开。关联方法:setGravity (int gravity)

android:horizontalSpacing

两列之间的间距。关联方法:setHorizontalSpacing(int)

android:numColumns

列数。关联方法:setNumColumns(int)

android:stretchMode

缩放模式。关联方法:setStretchMode(int)

android:verticalSpacing

两行之间的间距。关联方法:setVerticalSpacing(int)