3

我的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

4 回答 4

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 。因此,行每次都会被夸大并防止视图的回收。

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

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

安卓:cacheColorHint="#00000000"

于 2013-05-17T05:07:19.347 回答
6

尝试这个

     @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;
    }
于 2013-05-17T05:14:49.497 回答
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);  
}
}
于 2013-05-17T05:07:46.510 回答
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);
    }
于 2018-01-04T05:02:13.907 回答