繁体   English   中英

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

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

我有一个listview,我在其中显示文件和文件夹列表。 我正在使用我的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;
}

如果文件/文件夹被隐藏,我将列表项的背景颜色更改为hiddenColor,
(默认背景颜色为XML格式)

但在滚动时,它几乎将所有列表项背景颜色设置为hiddencolor。

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

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

您必须为每个项目增加布局,并返回新视图。 Listview对其他项使用相同的视图。

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

其他方法是在View holder中添加位置,然后检查

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

暂无
暂无

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

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