简体   繁体   English

listview项目的背景颜色在滚动时发生变化,

[英]listview item's background color changes on scrolling,

I have a listview , in which I am showing file and folder lists. 我有一个listview,我在其中显示文件和文件夹列表。 I am using my getView method as 我正在使用我的getView方法

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, 如果文件/文件夹被隐藏,我将列表项的背景颜色更改为hiddenColor,
(default background color is in XML) (默认背景颜色为XML格式)

But on scrolling it sets almost all list item background color as hiddencolor. 但在滚动时,它几乎将所有列表项背景颜色设置为hiddencolor。

I know this is due to listview recycling, but no idea how to resolve it. 我知道这是由于listview回收,但不知道如何解决它。

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. 您还必须设置非隐藏颜色,因为如果重复使用该视图,如果在转换视图之前设置了hiddenColor,则会获得hiddenColor。

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

在xml中放入android:fadingEdge="none"

尝试将其添加到xml文件中

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

You have to inflate layout for each item , and return a new view. 您必须为每个项目增加布局,并返回新视图。 Listview uses the same view for other items. Listview对其他项使用相同的视图。

remove the if(convertView == null) , so that each item will have different view object . 删除if(convertView == null),以便每个项目都有不同的视图对象。

Other way is to add the position in View holder , and check 其他方法是在View holder中添加位置,然后检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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