android - 加载和滚动时 Listview 行变得混合颜色

标签 android android-listview adapter baseadapter

我正在根据其中一个子项的文本值设置行的背景颜色。但是,无论其文本值如何,都会设置多个背景。上下滚动时情况会变得更糟。适配器的代码:

package com.test.app;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {

    private LayoutInflater mInflater = null;
    private ArrayList<String[]> mItems = new ArrayList<String[]>();

    public MyBaseAdapter(Context context, ArrayList<String[]> items) {
         mItems = items;
         mInflater = LayoutInflater.from(context);
    }

    public void addItem(String[] it) {
        mItems.add(it);
    }

    public void setListItems(ArrayList<String[]> lit) {
        mItems = lit;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        public TextView tv0,tv1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = null;
        ViewHolder viewHolder;

        if(convertView == null)
        {
            rowView = mInflater.inflate(R.layout.history_row, null);
        }
        else
        {
            rowView = convertView;
        }

        viewHolder = new ViewHolder();

        viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
        viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);

        rowView.setTag(viewHolder);

        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.tv0.setText(mItems.get(position)[0].toString());
        holder.tv1.setText(mItems.get(position)[1].toString());

        if(holder.tv1.getText().equals("0"))
        {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random rows.
        }

        return rowView;
    }

}

最佳答案

如果你只是使用:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
}

这确实会使包含 0 的行具有该特定颜色,但是当您上下滚动列表时,具有该颜色的该特定行将被回收并最终出现在它所在的位置不应该的。正确的方法是为不包含 0 的行提供默认颜色,以便覆盖可能的回收 View 的错误颜色:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
} else {
   rowView.setBackgroundColor(/*Here the default color will be*/)
   // This row doesn't contain 0 so it must have the default color.
  // Because you could be dealing with a recycled view(that has the above color)
 // then we must revert the color to the default to be sure we end up with the correct color.
}

ViewHolder 模式的正确代码是:

    View rowView = convertView;
    ViewHolder viewHolder;

    if(rowView == null) {
        rowView = mInflater.inflate(R.layout.history_row, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
        viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();    
    }             

然后您使用 viewHolder 对象来设置行的数据。

关于android - 加载和滚动时 Listview 行变得混合颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406701/

相关文章:

java - android/java桌面: decide what class to use depending on target

android - 找不到 Gradle DSL 错误 mavenCentral()

java - 如何设置ListView水平滚动?

java - 导致主要 Activity 的所有 Intent ?

android - Android终端模拟器中的权限被拒绝

Android 将颜色设置为 ListView 项

java - 如何在 Fragment 中自动刷新 ListView ?

android - ListView 在 notifyDataSetChanged 之后不会直观地更新其内容

android - 在android中过滤Realm对象

java - 从 Adapter 中的 Activity 调用方法