在尝试动态设置列表项中的 imageview
的可见性和图像时,我遇到了这个奇怪的问题 . 当它最初加载它的显示完全正常,但当我向上或向下滚动它时,一些图像不会显示 .
这是代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Alarm o = items.get(position);
if (convertView == null)
{
convertView = View.inflate(mcontxt, R.layout.facelistitem, null);
holder = new ViewHolder();
holder.ind = (ImageView) convertView.findViewById(R.id.imgind);
holder.name = (TextView) convertView.findViewById(R.id.txtname);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
if (o != null) {
String name;
StringBuilder strb = new StringBuilder().append(
pad(o.GetAlarmHour())).append(":").append(
pad(o.GetAlarmMinute()));
if (position == 0) {
name = "Add Alarm";
} else if ((o.getAlarmName() != null)
&& !o.getAlarmName().equals("")) {
name = o.getAlarmName().trim() + " - " + strb.toString();
} else {
name = strb.toString();
}
//TextView tt = (TextView) v.findViewById(R.id.txtname);
//ImageView iv = (ImageView) v.findViewById(R.id.imgind);
if (holder.name != null) {
holder.name.setText(name);
}
if (holder.ind != null) {
if (name.equalsIgnoreCase("Add Alarm"))
holder.ind.setVisibility(View.INVISIBLE);
if (o.IsAlarmOn())
holder.ind.setImageDrawable(mcontxt.getResources().getDrawable(
R.drawable.alarmon));
else
holder.ind.setImageDrawable(mcontxt.getResources().getDrawable(
R.drawable.alarmoff));
}
/*
* if(bt != null){ bt.setText("Status: "+ o.getOrderStatus()); }
*/
}
return convertView;
}
2 回答
整个_849394变黑了吗?您可能需要设置:
android:cacheColorHint="@android:color/transparent"
在
ListView
.否则,您可能需要提供
INVISIBLE
设置的替代方法:如果您不这样做,那么当您不打算使用时,回收的视图有时会是
INVISIBLE
.您需要做的就是在getView中将图像设置为VISIBLE,然后再将其设置为INVISIBLE / GONE(如有必要) .
在我的特定示例中: