您的位置:首页 > 移动开发 > Android开发

Android中GridVew的使用——使用自带的SimpleAdapter

2011-08-17 10:05 323 查看
GridView一直是一个系统登录后以九宫格方式展现功能子模块的最佳选择,经过试验和网上资料的查阅,现把实现方式总结一下:
一直是通过自定义Adapter方式,在getView()方法中设置图片的显示方式,这种方式资料比较多;
另一种方式是使用Android系统自带的Adapter:

首先,新建一个工程;

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

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <GridView

   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"/>

</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>

<ImageView

   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<String,Object>,为的是存储资源的名称(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<HashMap<String,Object>> imagelist = new ArrayList<HashMap<String,Object>>();

  
  // 使用HashMap将图片添加到一个数组中,注意一定要是HashMap<String,Object>类型的,因为装到map中的图片要是资源ID,而不是图片本身

  // 如果是用findViewById(R.drawable.image)这样把真正的图片取出来了,放到map中是无法正常显示的
  HashMap<String,Object> map1 = new HashMap<String,Object>();

  map1.put("image", R.drawable.gallery_photo_1);

  imagelist.add(map1);

  HashMap<String,Object> map2 = new HashMap<String,Object>();

  map2.put("image", R.drawable.gallery_photo_2);

  imagelist.add(map2);

  HashMap<String,Object> map3 = new HashMap<String,Object>();

  map3.put("image", R.drawable.gallery_photo_3);

  imagelist.add(map3);

  HashMap<String,Object> map4 = new HashMap<String,Object>();

  map4.put("image", R.drawable.gallery_photo_4);

  imagelist.add(map4);

  HashMap<String,Object> map5 = new HashMap<String,Object>();

  map5.put("image", R.drawable.gallery_photo_5);

  imagelist.add(map5);

  HashMap<String,Object> map6 = new HashMap<String,Object>();

  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);

 }

 

}



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息