1

我想使用以下代码为 ListView 中的项目着色:

holder.txtPosition = (TextView)row.findViewById(R.id.txtPosition); holder.txtPosition.setTextColor(0xFF00CC00);.

它工作得很好但是:它也每十个项目着色。例如:我为第一个项目设置颜色,所以每第十个项目也会获得颜色。

如果我向下滚动并再次向上滚动,就会有更多的颜色。

在这张图片中,我只为第一个“用户投票”文本着色,然后非常快速地向下滚动并滚动回顶部。

绿色文字

这是什么魔法?

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class List extends ArrayAdapter<ListItem>{

    Context context; 
    int layoutResourceId;    
    ListItem data[] = null;

    public List(Context context, int layoutResourceId, ListItem[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data; 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;
        ListItem entry = data[position];

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.txtVideos = (TextView)row.findViewById(R.id.txtVideos);
            holder.txtPosition = (TextView)row.findViewById(R.id.txtPosition);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtcomments = (TextView)row.findViewById(R.id.txtcomment);

            row.setTag(holder);

            if (entry.fav){
                //row.setBackgroundColor(0xFFfcf6bb);
            }
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }
        holder.txtTitle.setText(entry.teilnehmer);
        holder.txtPosition.setText(entry.position);

        int num = Integer.parseInt(entry.videos);
        if (num == 0 || num > 1){
            holder.txtVideos.setText(entry.videos + " Videos");
        } else {
            holder.txtVideos.setText(entry.videos + " Video");
        }

        if (holder.txtPosition.getText().equals("Ausgeschieden")){
            holder.txtPosition.setTextColor(0xFFFF0000);
        } else if(holder.txtPosition.getText().equals("User-Voting")){
            holder.txtPosition.setTextColor(0xFF00CC00);
        }

        num = Integer.parseInt(entry.comment);
        if (num == 0 || num > 1){
            holder.txtcomments.setText(entry.comment + " Kommentare");
        } else {
            holder.txtcomments.setText(entry.comment + " Kommentar");
        }
        return row;
    }

    static class WeatherHolder
    {
        TextView txtTitle;
        TextView txtVideos;
        TextView txtPosition;
        TextView txtcomments;
    }
}
4

1 回答 1

2

您正在回收您的 ListView 元素(这是一件好事)。但是当这样一个 View 被传递给你的getView方法时,它仍然处于它持有的最后一个元素的状态;因此,如果您指定setColor此值,也将应用于将注入容器的下一个文本。因此,您还应该在重新填充回收视图时重置颜色定义:

if (holder.txtPosition.getText().equals("Ausgeschieden")) {
    holder.txtPosition.setTextColor(0xFFFF0000);
} else if (holder.txtPosition.getText().equals("User-Voting")) {
    holder.txtPosition.setTextColor(0xFF00CC00);
} else {
    holder.txtPosition.setTextColor(R.color.your_default);
}
于 2013-01-24T12:02:32.347 回答