Android中GridView的使用——使用自带的SimpleAdapter(简单适配器)
GridView一直是一个系统登录后以九宫格方式展现功能子模块的最佳选择,经过试验和网上资料的查阅,现把实现方式总结一下:
一直是通过自定义Adapter方式,在getView()方法中设置图片的显示方式,这种方式资料比较多;
另一种方式是使用Android系统自带的Adapter:
具体步骤如下:
1.新建一个安卓工程:
2.定义一个布局文件,可以命名为gridview.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" >
<GridView
android:id="@+id/GridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="4"
/> </LinearLayout>
3.定义一个项布局文件items.xml,目的是为了展现GridView中具体子项item的显示方式
代码如下:
<?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/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10px"
android:scaleType="fitCenter" /> <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5px" /> </LinearLayout>
3.好了,定义好了界面,我们就可以写程序代码了~
在主Activity中定义一个数组,数组类型为HashMap<String,Object>,为的是存储资源的名称(key)和资源的值(value),这里假设资源就是图片
然后,将资源加入到这个类型为HashMap的数组中去,并使用SimpleAdapter来实现数据的展现即可,下面上代码:
具体代码如下:
package com.flysnow.sina.weibo; import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter; import com.flysnow.sina.weibo.R; public class MyListActivity extends Activity {
private int[] image = { R.drawable.bc2, R.drawable.bc2, R.drawable.bc2,
R.drawable.bc2, R.drawable.bc2, R.drawable.bc2, R.drawable.bc2,
R.drawable.bc2 };
private String[] text = { "a", "b", "c", "d", "e", "f", "g", "h" }; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main1);
//显示GridView的界面
GridView gridview = (GridView) findViewById(R.id.GridView1);
ArrayList<HashMap<String, Object>> imagelist = new ArrayList<HashMap<String, Object>>();
// 使用HashMap将图片添加到一个数组中,注意一定要是HashMap<String,Object>类型的,因为装到map中的图片要是资源ID,而不是图片本身
// 如果是用findViewById(R.drawable.image)这样把真正的图片取出来了,放到map中是无法正常显示的
for (int i = 0; i < 8; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("image", image[i]);
map.put("text", text[i]);
imagelist.add(map);
}
// 使用simpleAdapter封装数据,将图片显示出来
// 参数一是当前上下文Context对象
// 参数二是图片数据列表,要显示数据都在其中
// 参数三是界面的XML文件,注意,不是整体界面,而是要显示在GridView中的单个Item的界面XML
// 参数四是动态数组中与map中图片对应的项,也就是map中存储进去的相对应于图片value的key
// 参数五是单个Item界面XML中的图片ID
SimpleAdapter simpleAdapter = new SimpleAdapter(this, imagelist,
R.layout.items, new String[] { "image", "text" }, new int[] {
R.id.image, R.id.title });
// 设置GridView的适配器为新建的simpleAdapter
gridview.setAdapter(simpleAdapter);
}
}
效果图:
Android中GridView的使用——使用自带的SimpleAdapter(简单适配器)的更多相关文章
- Android中GridView通过自定义适配器(未优化)实现图文视图排列
Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...
- Android中GridView拖拽的效果【android进化三十六】
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...
- Android中GridView拖拽的效果
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的那么 ...
- Android中GridView的实现实例
实现效果: activity文件代码: package com.tmacsky; import android.app.Activity; import android.os.Bundle; impo ...
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...
- android中GridView关于间距的属性值介绍
android:columnWidth 设置列的宽度.关联的方法为:setColumnWidth(int) stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间 ...
- Android中GridView、ListView 的 getChildAt() 方法返回null 问题
开发的Android app用到了GridView或者ListView,通常使用getChildAt(int position)方法获取当前点击或者选中的View(即position对应的View). ...
- Android中启动页ViewPager和ViewFlipper带指示器
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 首先我们来分析一下,想要实现启动页的功能,大家第一个想到的就是使用ViewPager,使用ViewPager确实是一种比 ...
- Android中GridView滚动到底部加载数据终极版
之前在项目中有一个需求是需要GridView控件,滚动到底部自动加载.但是呢GridView控件并不提供诸如ListView监听滚动到底部的onScrollListener方法,为了实现这样一个效果, ...
随机推荐
- 前自加(++a)与后自加(a++)的差别
自加是自己加1的操作.比如a++ 是a+1 ,变量a变成了 a+1的值. 如果是简单的只做 a++:或者++a的语句,效果是一样的. 但是如果嵌入到复杂的语句中,比如 b = a++: 和 b = + ...
- Linux下Apache重启遇到No space left on device错误的解决方法
解决办法:1.输入:ipcs -s 看有没有超过5个,如果有请执行下面2的命令:2.ipcs -s | perl -ane '/^0x00000000/ && `ipcrm -s ...
- Hadoop基础概念介绍
基于YARN的配置信息, 参见: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ hadoop入门 - 基础概念 ...
- HDU 4498 Function Curve (分段, simpson)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 最近太逗了...感觉成都要打铁了...只能给队友端 ...
- ImageView 各种工具类
package imageUtil; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOut ...
- This 关键字和变量作用域
public class Number { int count; public void method01(){ // int count=3; count=3; // t ...
- js静态属性和静态方法
啥也不说了,直接上代码...(就是这么任性) var Animal = function(){}; Animal.name = "dog"; var a = ...
- PCB成型製程介紹
PCB成型製程在電子構裝中所扮演的角色 下圖是電腦主機的內部組成 我們將以插在主機板上的一片 USB擴充卡來說明PCB成型製 程在電子構裝中所扮演的角色 PCB成型製程的子製程 USB擴充卡要插入主機 ...
- Mac 键盘快捷键
标签页和窗口快捷键 ⌘-N 打开新窗口. ⌘-T 打开新标签页. ⌘-Shift-N 在隐身模式下打开新窗口. 按 ⌘-O,然后选择文件. 在 Google Chrome 浏览器中打开计算机中的文件. ...
- Select XML Nodes by Name [C#]
原文 http://www.csharp-examples.net/xml-nodes-by-name/ To find nodes in an XML file you can use XPath ...