Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listview item's background color changes on scrolling,

I have a listview , in which I am showing file and folder lists. I am using my getView method as

static class ViewHolder {
    protected TextView text1;
    protected TextView text2;
}

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

    ViewHolder viewHolder = null;

    if(convertView == null){

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
        convertView = inflater.inflate(R.layout.row, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.text1 = (TextView) convertView.findViewById(R.id.text1);
        viewHolder.text2 = (TextView) convertView.findViewById(R.id.text2);

        convertView.setTag(viewHolder);

    }
    else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.text1.setText(itemsArrayList.get(position).getFileName());
    viewHolder.text2.setText(itemsArrayList.get(position).getSize());

    <if( itemsArrayList.get(position).isHidden() ) {
        convertView.setBackgroundColor(context.getResources().getColor(R.color.hiddenColor));
    }

    return convertView;
}

If file/folder is hidden , I am changing background color of list item as hiddenColor,
(default background color is in XML)

But on scrolling it sets almost all list item background color as hiddencolor.

I know this is due to listview recycling, but no idea how to resolve it.

like image 230
Pratapi Hemant Patel Avatar asked Mar 20 '23 01:03

Pratapi Hemant Patel


1 Answers

You have to set the not hidden color too, because if the view is reused you will get the hiddenColor if it was set before to that convert view.

if( itemsArrayList.get(position).isHidden() ) {
    convertView.setBackgroundColor(context.getResources().getColor(R.color.hiddenColor));
} else {
   **convertView.setBackgroundColor(Put your other color here)**
}
like image 198
Alex Avatar answered Apr 02 '23 22:04

Alex