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

时间:2013-05-17 05:03:44

标签: android android-listview

我的ListView包含两个Textviews。在一行中,第一行是名称,第二行是结果。我需要根据结果更改结果TextView的背景颜色。就像传递一样,结果TextView颜色将为绿色,如果失败,结果TextView颜色将为红色。我的ListView中有十行。我试过这样的

    @Override
public 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;
}

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

4 个答案:

答案 0 :(得分:7)

我认为这是因为列表视图会回收视图,从而导致出现此类问题。请尝试以下操作:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = null;
        convertView = null;
        row = convertView;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) _context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row, parent,
                    false);
                       // your code 


          }
     return row;
}

由于转换视图和行视图都初始化为null .Hence行每次都会被充气并阻止视图的回收。

链接: Listview android recycling 此链接解释了回收视图的机制。

  • 如果错误的视图正在改变颜色,请使用上面的代码。
  • 如果您在触摸视图时尝试说,而滚动背景变为黑色,请在列表视图中使用以下内容
  

机器人:cacheColorHint = “#00000000”

答案 1 :(得分:5)

试试这个

     @Override
    public 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());


//**********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;
    }

答案 2 :(得分:0)

你必须做这样的事情,

if(Yourmark[position]<50) //Where Yourmark[] is the mark array you are passing to your custom adapter
{
        holder.img.setImageResource(R.drawable.red_arrow);
        holder.txtViewDescription.setBackgroundResource(R.color.resultred);  
        holder.txtholder.setBackgroundResource(R.color.resultred);  
}

else
{
        holder.img.setImageResource(R.drawable.green_arrow);
        holder.txtViewDescription.setBackgroundResource(R.color.resultgreen);  
        holder.txtholder.setBackgroundResource(R.color.resultgreen);  
}
}

答案 3 :(得分:0)

在适配器(自定义)上设置此代码,以便仅在选择视图时才能使用以前的颜色回收视图。

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE );

    Task task = taskArrayList.get(position);

    View view = inflater.inflate(R.layout.task_row, parent, false);

    if(MyActivity.getIsClicked() && MyActivity.getPositionClicked() == position){
        view.setBackgroundResource(R.color.backgroundSelectedItem);
    }