4

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.

Hemant Patel
  • 3,160
  • 1
  • 20
  • 29
  • check this for reference http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct. Changes text and color on click. – Raghunandan Mar 28 '14 at 13:18

4 Answers4

7

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)**
}
Alex
  • 3,382
  • 2
  • 32
  • 41
0

put android:fadingEdge="none" in xml

Lucian Novac
  • 1,255
  • 12
  • 18
0

try adding this to the xml file

android:cacheColorHint="@android:color/transparent"
0

You have to inflate layout for each item , and return a new view. Listview uses the same view for other items.

remove the if(convertView == null) , so that each item will have different view object .

Other way is to add the position in View holder , and check

 if(convertView == null || contentView.getTag().position != position)
Libin
  • 16,967
  • 7
  • 61
  • 83
  • If we will inflate each item each time it will gives memory out exception and app will crash after some scrolling. I had tried this one earlier. – Hemant Patel Mar 28 '14 at 13:34
  • No list view will keep hold of items which are only in memory. This kind of approach is used in advanced list items which handles different action( e.g., say if an list item has to scroll its test inside) – Libin Mar 28 '14 at 13:37