ListView - SimpleAdapter 行间颜色交替(转)
一、概述
通过扩展SimpleAdapter,来改变显示外观。因为要每行的显示颜色,首先要获得每行的View实例,然后调用setBackgroundColor函数设置。
二、实例
【效果】
【代码片段】
- package enleo.ListViewA;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.SimpleAdapter;
- public class SpecialAdapter extends SimpleAdapter{
- private int[] colors = new int[]{0x30FF0000, 0x300000FF};
- public SpecialAdapter(Context context, List<? extends Map<String, ?>> data,
- int resource, String[] from, int[] to) {
- super(context, data, resource, from, to);
- // TODO Auto-generated constructor stub
- }
- /* (non-Javadoc)
- * @see android.widget.SimpleAdapter#getView(int, android.view.View, android.view.ViewGroup)
- */
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- View view = super.getView(position, convertView, parent);
- int colorPos = position%colors.length;
- view.setBackgroundColor(colors[colorPos]);
- return view;
- }
- }