滚动时Android-Listview项目背景颜色发生变化

滚动时Android-Listview项目背景颜色发生变化,第1张

概述我的ListView包含两个Textview.在一行中,第一行是名称,第二行是结果.我需要根据结果更改结果TextView的背景颜色.就像传递然后结果TextView颜色将是绿色失败结果TextView颜色将是红色.我的ListView中有十行.我试过这样的 @Overridepublic View getView(int position, View convertView, ViewGrou 我的ListVIEw包含两个TextvIEw.在一行中,第一行是名称,第二行是结果.我需要根据结果更改结果TextVIEw的背景颜色.就像传递然后结果TextVIEw颜色将是绿色失败结果TextVIEw颜色将是红色.我的ListVIEw中有十行.我试过这样的

@OverrIDepublic VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {    // Todo auto-generated method stub    VIEwHolder holder;    if(convertVIEw==null)    {        holder = new VIEwHolder();        convertVIEw = inflater.inflate(R.layout.items,null);        holder.imgVIEwlogo = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imgVIEwlogo);        holder.txtVIEwTitle = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txtVIEwTitle);        holder.txtVIEwDescription = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txtVIEwDescription);        holder.txtholder = (TextVIEw) convertVIEw.findVIEwByID(R.ID.holder1);        holder.img = (ImageVIEw)  convertVIEw.findVIEwByID(R.ID.temperrr);        convertVIEw.setTag(holder);    }    else        holder=(VIEwHolder)convertVIEw.getTag();    ItemBean bean = (ItemBean) itemList.get(position);    holder.imgVIEwlogo.setimageResource(bean.getimage());    holder.txtVIEwTitle.setText(bean.getTitle());    holder.txtVIEwDescription.setText(bean.getDescription());  //------------------      if (position==0)    {        if(GrIDvIEwAdapter.glu>=81)        {        holder.img.setimageResource(R.drawable.red_arrow);        holder.txtVIEwDescription.setBackgroundResource(R.color.resultred);          holder.txtholder.setBackgroundResource(R.color.resultred);          }        else if (GrIDvIEwAdapter.glu==79||GrIDvIEwAdapter.glu==80)        {            holder.img.setimageResource(R.drawable.orange_arrow);            holder.txtVIEwDescription.setBackgroundResource(R.color.resultoren);              holder.txtholder.setBackgroundResource(R.color.resultoren);          }        else        {            holder.img.setimageResource(R.drawable.resultarrawnocolor);            holder.txtVIEwDescription.setBackgroundResource(0);              holder.txtholder.setBackgroundResource(0);          }    }        holder.img.setimageResource(R.drawable.resultarrawnocolor);       return convertVIEw;}

但是,当滚动列表时,另一个随机项目的背景颜色会发生变化.我必须做什么.如何控制列表视图中每个位置的项目.以上代码仅适用于第一行.我对吗?

解决方法 试试这个

@OverrIDe    public VIEw getVIEw(int position,VIEwGroup parent) {        // Todo auto-generated method stub        VIEwHolder holder;        if(convertVIEw==null)        {            holder = new VIEwHolder();            convertVIEw = inflater.inflate(R.layout.items,null);            holder.imgVIEwlogo = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imgVIEwlogo);            holder.txtVIEwTitle = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txtVIEwTitle);            holder.txtVIEwDescription = (TextVIEw) convertVIEw.findVIEwByID(R.ID.txtVIEwDescription);            holder.txtholder = (TextVIEw) convertVIEw.findVIEwByID(R.ID.holder1);            holder.img = (ImageVIEw)  convertVIEw.findVIEwByID(R.ID.temperrr);            convertVIEw.setTag(holder);        }        else            holder=(VIEwHolder)convertVIEw.getTag();        ItemBean bean = (ItemBean) itemList.get(position);        holder.imgVIEwlogo.setimageResource(bean.getimage());        holder.txtVIEwTitle.setText(bean.getTitle());        holder.txtVIEwDescription.setText(bean.getDescription());//**********SET ALSO YOUR DEFAulT BACKGROUND HERE******** like//     holder.txtVIEwDescription.setBackgroundResource(R.color.DEFAulcolor);       holder.txtholder.setBackgroundResource(R.color.DEFAulcolor);      holder.img.setimageResource(R.drawable.defalut_image);//**********SET ALSO YOUR DEFAulT BACKGROUND HERE******** ok//      //------------------          if (position==0)        {            if(GrIDvIEwAdapter.glu>=81)            {            holder.img.setimageResource(R.drawable.red_arrow);            holder.txtVIEwDescription.setBackgroundResource(R.color.resultred);              holder.txtholder.setBackgroundResource(R.color.resultred);              }            else if (GrIDvIEwAdapter.glu==79||GrIDvIEwAdapter.glu==80)            {                holder.img.setimageResource(R.drawable.orange_arrow);                holder.txtVIEwDescription.setBackgroundResource(R.color.resultoren);                  holder.txtholder.setBackgroundResource(R.color.resultoren);              }            else            {                holder.img.setimageResource(R.drawable.resultarrawnocolor);                holder.txtVIEwDescription.setBackgroundResource(0);                  holder.txtholder.setBackgroundResource(0);              }        }            holder.img.setimageResource(R.drawable.resultarrawnocolor);           return convertVIEw;    }
总结

以上是内存溢出为你收集整理的滚动时Android-Listview项目背景颜色发生变化全部内容,希望文章能够帮你解决滚动时Android-Listview项目背景颜色发生变化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122963.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存