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

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        LinearLayout root_view = (LinearLayout) row
                .findViewById(R.id.list_root_view);

        int evenRow = position % 2;
        if (evenRow == 0) {
            root_view.setBackgroundColor(context.getResources().getColor(
                    android.R.color.darker_gray));
        }

        holder = new ViewHolder();
        holder.date = (TextView) row.findViewById(R.id.dateTextView);
        holder.time = (TextView) row.findViewById(R.id.timeTextView);
        holder.breachNo = (TextView)row.findViewById(R.id.breachNoTextView);
        holder.fee = (TextView) row.findViewById(R.id.feeTextView);
        holder.orderCheckBox =CheckBox)row.findViewById(R.id.orderCheckBox);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    historyData = arraylist.get(position);
    holder.date.setText(historyData.getDate() + "");
    holder.time.setText(historyData.getTime().toString());
    holder.breachNo.setText(historyData.getBreachNo() + "");
    holder.fee.setText(historyData.getFee() + "");
    holder.orderCheckBox.setEnabled(true);

    return row;
}

在上面的代码中我有一些问题。我想listview交替设置我的项目颜色。(例如蓝色的奇数行和灰色的偶数行)。在加载活动时,我得到了完美的颜色。问题是滚动我 listview的项目颜色随机变化。谁能帮我解决..?

4

2 回答 2

2

获取视图

设置背景颜色,在 setTag 之外,如果 else 块。是的,也可以添加 else 。

喜欢

if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        LinearLayout root_view = (LinearLayout) row
                .findViewById(R.id.list_root_view);

        int evenRow = position % 2;
        if (evenRow == 0) {
            root_view.setBackgroundColor(context.getResources().getColor(
                    android.R.color.darker_gray));
        }

        holder = new ViewHolder();
        holder.date = (TextView) row.findViewById(R.id.dateTextView);
        holder.time = (TextView) row.findViewById(R.id.timeTextView);
        holder.breachNo = (TextView)row.findViewById(R.id.breachNoTextView);
        holder.fee = (TextView) row.findViewById(R.id.feeTextView);
        holder.orderCheckBox =CheckBox)row.findViewById(R.id.orderCheckBox);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

int evenRow = position % 2;
        if (evenRow == 0) {
            root_view.setBackgroundColor(context.getResources().getColor(
                    android.R.color.darker_gray));
        } else {
              root_view.setBackgroundColor(context.getResources().getColor(
                    android.R.color.blue));

}............

于 2013-03-13T06:08:09.717 回答
0

您可以使用

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

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

            holder = new ViewHolder();
            holder.root_view = (LinearLayout) row
            .findViewById(R.id.list_root_view);
            holder.date = (TextView) row.findViewById(R.id.dateTextView);
            holder.time = (TextView) row.findViewById(R.id.timeTextView);
            holder.breachNo = (TextView)row.findViewById(R.id.breachNoTextView);
            holder.fee = (TextView) row.findViewById(R.id.feeTextView);
            holder.orderCheckBox =CheckBox)row.findViewById(R.id.orderCheckBox);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        int evenRow = position % 2;
        historyData = arraylist.get(position);
        holder.date.setText(historyData.getDate() + "");
        holder.time.setText(historyData.getTime().toString());
        holder.breachNo.setText(historyData.getBreachNo() + "");
        holder.fee.setText(historyData.getFee() + "");
        holder.orderCheckBox.setEnabled(true);

        if (evenRow == 0) {
            holder.root_view.setBackgroundColor(context.getResources().getColor(
                        android.R.color.darker_gray));
            }
        else
            holder.root_view.setBackgroundColor(context.getResources().getColor(
                        android.R.color.blue));

        return row;
    }

你也应该LinearLayoutViewHolder课堂上添加你的

class ViewHolder{
        LinearLayout root_view;
        ...
        ...
        ...
    }
于 2013-03-13T06:28:53.383 回答