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方法,为了实现这样一个效果, ...
随机推荐
- C++中的string
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- discuz_style_default.xml 修改
<?xml version="1.0" encoding="ISO-8859-1"?> <root> <item id=" ...
- WA(Write Amplification)写入放大
WA是闪存及SSD相关的一个极为重要的属性.由于闪存必须先擦除才能再写入的特性,在执行这些操作时,数据都会被移动超过1次.这些重复的操作不单会增加写入的数据量,还会减少闪存的寿命,更吃光闪存的可用带宽 ...
- 将 SQL Server 实例设置为自动启动(SQL Server 配置管理器)
本主题说明如何使用 SQL Server 配置管理器在 SQL Server 2012 中将 SQL Server 实例设置为自动启动. 在安装过程中,SQL Server 通常配置为自动启动. 如果 ...
- Android Activity和Fragment传递数据
1.Activity与Activity传递数据 UserLoginActivity.java: Intent welcomePage = new Intent(); Bundle dataBundle ...
- 虚拟机的静态内部 IP 地址
这是什么? 借助最新的 PowerShell 版本,您现在能够定义和配置特定的内部 IP 地址,该地址可以静态分配给部署在虚拟网络中的 IaaS 虚拟机.使用此功能,您可以直接为虚拟机配置内部 ...
- Codeforces 701C They Are Everywhere(Two pointers+STL)
[题目链接] http://codeforces.com/problemset/problem/701/C [题目大意] 给出 一个字符串,里面包含一定种类的字符,求出一个最短的子串,使得其包含该字符 ...
- Oracle 11g RAC database on ASM, ACFS or OCFS2
I see a lot of questions on shared file systems that can be used when people move from single instan ...
- OpenGL 4 : 一个漂亮的心 For you, My Love
画一个心形有非常多公式能够使用,以下这个公式我觉得最完美了: float x = R * 16 * pow(sin(theta), 3); float y = R * (13 * cos(theta) ...
- nginx 使用安装问题及解决方案
如何安装清参考:http://www.runoob.com/linux/nginx-install-setup.html 可以先不用配置,等理解后在配置. 在启动nginx时,出现提示: nginx: ...