设为首页 加入收藏

TOP

Android中GridView的使用——使用自带的SimpleAdapter
2014-11-24 11:10:55 来源: 作者: 【 】 浏览:0
Tags:Android GridView 使用 自带 SimpleAdapter

GridView一直是一个系统登录后以九宫格方式展现功能子模块的最佳选择,经过试验和网上资料的查阅,现把实现方式总结一下:


一直是通过自定义Adapter方式,在getView()方法中设置图片的显示方式,这种方式资料比较多;


首先,新建一个工程;


定义一个主界面XML,如gridview.xml


< xml version="1.0" encoding="utf-8" >
xmlns:android="
http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"/>


同时,定义一个要展现在GridView中的具体子项Item的界面,simple_grid_item.xml


< xml version="1.0" encoding="utf-8" >
xmlns:android="
http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"/>


好了,定义好了界面,我们就可以写程序代码了~


在主Activity中定义一个数组,数组类型为HashMap,为的是存储资源的名称(key)和资源的值(value),这里假设资源就是图片


然后,将资源加入到这个类型为HashMap的数组中去,并使用SimpleAdapter来实现数据的展现即可,下面上代码:


public class MyGridView extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.gridview);
// 显示GridView的界面
GridView gridView = (GridView)findViewById(R.id.gridview);
ArrayList> imagelist = new ArrayList>();

// 使用HashMap将图片添加到一个数组中,注意一定要是HashMap类型的,因为装到map中的图片要是资源ID,而不是图片本身
// 如果是用findViewById(R.drawable.image)这样把真正的图片取出来了,放到map中是无法正常显示的
HashMap map1 = new HashMap();
map1.put("image", R.drawable.gallery_photo_1);
imagelist.add(map1);
HashMap map2 = new HashMap();
map2.put("image", R.drawable.gallery_photo_2);
imagelist.add(map2);
HashMap map3 = new HashMap();
map3.put("image", R.drawable.gallery_photo_3);
imagelist.add(map3);
HashMap map4 = new HashMap();
map4.put("image", R.drawable.gallery_photo_4);
imagelist.add(map4);
HashMap map5 = new HashMap();
map5.put("image", R.drawable.gallery_photo_5);
imagelist.add(map5);
HashMap map6 = new HashMap();
map6.put("image", R.drawable.gallery_photo_6);
imagelist.add(map6);

// 使用simpleAdapter封装数据,将图片显示出来
// 参数一是当前上下文Context对象
// 参数二是图片数据列表,要显示数据都在其中
// 参数三是界面的XML文件,注意,不是整体界面,而是要显示在GridView中的单个Item的界面XML
// 参数四是动态数组中与map中图片对应的项,也就是map中存储进去的相对应于图片value的key
// 参数五是单个Item界面XML中的图片ID
SimpleAdapter simpleAdapter = new SimpleAdapter(this, imagelist, R.layout.simple_grid_item, new String[] {"image"}, new int[]{R.id.image});


// 设置GridView的适配器为新建的simpleAdapter
gridView.setAdapter(simpleAdapter);
}



}



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android原理揭秘系列之VacantCell.. 下一篇Android应用程序的Activity启动过..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)
·C语言指南:C语言内 (2025-12-27 06:23:14)
·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)