一、GridView是以网格的形式排列元素,方式与矩阵类似,下面通过程序来了解GridView的使用
二、activity_main.xml中的实现代码如下
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/imagelist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="20dp"
android:numColumns="3"
android:verticalSpacing="10dp" >
</GridView>
<ImageView
android:id="@+id/imageshow"
android:layout_below="@+id/imagelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:adjustViewBounds="true" />
</RelativeLayout>
在layout文件夹中新建一个grid.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" >
<ImageView
android:id="@+id/smallImage"
android:layout_width="80dp"
android:layout_height="50dp"/>
</LinearLayout>
MainActivity.java中的代码实现如下
package abc.edu.cn.gridview;
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.test.suitebuilder.annotation.SmallTest;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private GridView gv;// 网格
private ImageView showImage;// 显示大图片
private int[] pics;// 存放图片Id的数组
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 查找到在布局中定义组件
gv = (GridView) this.findViewById(R.id.imagelist);
showImage = (ImageView) this.findViewById(R.id.imageshow);
// 实例化图片Id的数组
pics = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4, R.drawable.p5, R.drawable.p6 };
// 创建一个List<Map>集合,用于将图片Id转换为List集合
List<Map<String, Object>> pic_items = new ArrayList<Map<String, Object>>();
for (int pic_id : pics) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("pic", pic_id);
pic_items.add(item);
}
// 创建SimpleAdapter适配器,以备与GridView组件绑定
SimpleAdapter adapter = new SimpleAdapter(this, pic_items,
R.layout.grid, new String[] { "pic" },
new int[] { R.id.smallImage });
// GridView组件与adapter适配器进行绑定
gv.setAdapter(adapter);
// 为showImage组件设置默认图片
showImage.setImageResource(pics[0]);
// 注册事件监听
gv.setOnItemClickListener(new GridViewHandler());
}
public class GridViewHandler implements OnItemClickListener {
/**
* AdapterView adpaterview:发生点击事件的AdapterView;
* View view:AdapterView中被用户点击的Item(GridView中的Item);
* int positon:被点击的Item在Adapter中的位置 ;
* long id:被点击的Item的Id
*/
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
showImage.setImageResource(pics[position]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
三、程序运行结果如下