简体   繁体   中英

Listview rows getting mixed colors when loading and scrolling

I'm setting the background color of a row based on a text value of one of it's children. However, multiple backgrounds are being set regardless of its text value. It gets worse when scrolling up and down. The code for the adapter:

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 you just use:

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

this will indeed make the row containing 0 to have that particular color but as you will scroll the list up and down, this particular row with this color will get recycled and end up in places where it shouldn't be. The correct way is to provide a default color to the row if it doesn't contain 0 so you override the bad color of a possible recycled 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.
}

Also the correct code for the ViewHolder pattern is:

    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();    
    }             

and then you use the viewHolder object to setup the row's data.

你可以尝试一下,设置为ListView: android:cacheColorHint="#00000000"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM