2014-03-28 104 views
2

我有一个列表视图,其中我显示文件和文件夹列表。 我用我的getView方法listview项目的滚动背景颜色变化,

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

如果文件/文件夹是隐藏的,我改变列表项的hiddenColor的背景颜色,
(默认的背景色是XML)

但在滚动它将几乎所有的列表项目背景颜色设置为隐藏颜色。

我知道这是由于listview回收,但不知道如何解决它。

+0

检查这个参考http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct。点击时更改文字和颜色。 – Raghunandan

回答

6

您必须设置非隐藏颜色,因为如果视图被重用,您将在转换视图之前设置hiddenColor。

if(itemsArrayList.get(position).isHidden()) { 
    convertView.setBackgroundColor(context.getResources().getColor(R.color.hiddenColor)); 
} else { 
    **convertView.setBackgroundColor(Put your other color here)** 
} 
+0

谢谢,它工作 –

0

android:fadingEdge="none"在XML

+0

这已弃用 –

0

尝试添加该XML文件

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

我早些时候尝试过,但它不起作用 –

0

你有夸大布局为每个项目,并返回一个新的视角。 Listview对其他项目使用相同的视图。

删除if(convertView == null),以便每个项目都有不同的视图对象。

其他的方法是添加在查看持有人的位置,并检查

if(convertView == null || contentView.getTag().position != position) 
+0

如果我们将每个项目膨胀每次它会给内存异常和应用程序将滚动一些后崩溃。 我曾经尝试过这个。 –

+0

没有列表视图将保持只在内存中的项目。这种方法用于处理不同动作的高级列表项(例如,如果列表项必须在内部滚动其测试) – Libin