android表格效果之ListView隔行变色实现代码
程序员文章站
2023-11-30 23:22:16
首先继承simpleadapter 复制代码 代码如下: package meetweb.net.util; import java.util.list; import j...
首先继承simpleadapter
package meetweb.net.util;
import java.util.list;
import java.util.map;
import android.content.context;
import android.graphics.color;
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);
}
@override
public view getview(int position ,view convertview,viewgroup parent){
view view=super.getview(position, convertview, parent);
int colorpos=position%colors.length;
if(colorpos==1)
view.setbackgroundcolor(color.argb(250, 255, 255, 255)); //颜色设置
else
view.setbackgroundcolor(color.argb(255, 224, 243, 250));//颜色设置
return view; } }
第二,使用重载的adapter来达到效果
import meetweb.net.util.specialadapter;
。。。。。
private specialadapter simpleadapter = null;
public void showdata(){
ratelist = rateservice.findall();
system.out.println(ratelist);
lvrate=(listview) this.findviewbyid(r.id.lvrate);
simpleadapter = new specialadapter(this, ratelist, r.layout.accuratelistitem, new string[]{"yearlimit","year1","year2"},
new int[]{r.id.tv_yearlimit,r.id.tv_year1,r.id.tv_year2});
lvrate.setadapter(simpleadapter);
//listview.setonitemclicklistener(listener);
}
其实主要是需要重载simpleadapter,本人引用网络观点进行编写
复制代码 代码如下:
package meetweb.net.util;
import java.util.list;
import java.util.map;
import android.content.context;
import android.graphics.color;
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);
}
@override
public view getview(int position ,view convertview,viewgroup parent){
view view=super.getview(position, convertview, parent);
int colorpos=position%colors.length;
if(colorpos==1)
view.setbackgroundcolor(color.argb(250, 255, 255, 255)); //颜色设置
else
view.setbackgroundcolor(color.argb(255, 224, 243, 250));//颜色设置
return view; } }
第二,使用重载的adapter来达到效果
复制代码 代码如下:
import meetweb.net.util.specialadapter;
。。。。。
private specialadapter simpleadapter = null;
public void showdata(){
ratelist = rateservice.findall();
system.out.println(ratelist);
lvrate=(listview) this.findviewbyid(r.id.lvrate);
simpleadapter = new specialadapter(this, ratelist, r.layout.accuratelistitem, new string[]{"yearlimit","year1","year2"},
new int[]{r.id.tv_yearlimit,r.id.tv_year1,r.id.tv_year2});
lvrate.setadapter(simpleadapter);
//listview.setonitemclicklistener(listener);
}
其实主要是需要重载simpleadapter,本人引用网络观点进行编写