【原创】自定义SimpleAdapter(一)
来源:互联网 发布:信阳师范网络教学空间 编辑:程序博客网 时间:2024/06/05 21:52
学习Android编程的各位同学,如果用过listView的话,应该一定知道SimpleAdapter吧,但是系统自带的SimpleAdapter功能有限,有事无法满足我们的需求,这就需要我们来自定义属于自己的SimpleAdapter。举个简单的小例子,就是实现列表背景颜色的交替。效果如下:
下面讲讲我是如何实现的。
其实很简单,就是重载SimpleAdapter的getView函数。代码如下:
定义一个int型数组mColors,保存想要显示的所有颜色。 在getView函数中,我们知道现在显示的item的position,利用position % colorLength就可以求出item项对应的颜色。然后利用系统的super.getView(position, convertView, parent)函数获得View实例,设置它的背景颜色就可以了,还是很简单吧。
今天写程序的时候,遇到了这个问题,特留下此贴,与大家分享。
其实很简单,就是重载SimpleAdapter的getView函数。代码如下:
public class AltColorAdapter extends SimpleAdapter {
private static int[] mColors = { R.drawable.grey, R.drawable.white };
public AltColorAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int[] arrayOfInt = mColors;
int colorLength = mColors.length;
int selected = arrayOfInt[position % colorLength];
View localView = super.getView(position, convertView, parent);
localView.setBackgroundResource(selected);
return localView;
}
}
private static int[] mColors = { R.drawable.grey, R.drawable.white };
public AltColorAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int[] arrayOfInt = mColors;
int colorLength = mColors.length;
int selected = arrayOfInt[position % colorLength];
View localView = super.getView(position, convertView, parent);
localView.setBackgroundResource(selected);
return localView;
}
}
今天写程序的时候,遇到了这个问题,特留下此贴,与大家分享。
0 0
- 【原创】自定义SimpleAdapter(一)
- 自定义SimpleAdapter(一)
- 自定义SimpleAdapter
- 自定义SimpleAdapter
- 自定义SimpleAdapter
- 自定义SimpleAdapter
- 自定义SimpleAdapter
- 自定义PopupWindow+SimpleAdapter+Anim
- SimpleAdapter及自定义
- List控件使用---SimpleAdapter使用详解(一)
- List控件使用---SimpleAdapter使用详解(一)
- List控件使用---SimpleAdapter使用详解(一)
- List控件使用---SimpleAdapter使用详解(一)
- 重写SimpleAdapter实现自定义ListView
- android自定义动态数据SimpleAdapter
- [原创] Questioning C# (一)
- [原创] Delphi(一) --探索
- 原创实用代码(一)
- 苹果内购遇到的坑
- 2011年父亲节
- python3的tempfile模块
- Ubuntu下开设FTP服务器
- yum 本地库离线安装
- 【原创】自定义SimpleAdapter(一)
- yum 本地库离线安装源码
- C++中用冒泡排序算法对一个数组中的元素进行排序
- java源文件编译后产生byte code (字节码)
- 蓝桥杯-最小乘积-贪心-java
- Lintcode 在O(1)时间复杂度删除链表节点
- yum错误集
- 【原创】CoolFormat源代码格式化工具
- 重要元素