列表项中的 Android 列表视图图像视图在滚动时隐藏
当尝试动态设置列表项中 imageview
的可见性和图像 drawable
时,我遇到了这个奇怪的问题。当它最初加载时,它的显示完全正常,但是当我向上或向下滚动它时,某些图像不会显示。
这是代码:
@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;
}
I am having this weird problem when trying to dynamically set visibility and image drawable
of imageview
in list item. When it initially loads its shows up completely fine but when I scroll it up or down some of the images doesn't show up.
Here is the code:
@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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
整个
ListView
变黑了吗?您可能需要设置:
android:cacheColorHint="@android:color/transparent"
在
ListView
上。否则,您可能需要提供
INVISIBLE
设置的替代方案:如果您不这样做,那么当您不希望回收的视图这样做时,它们有时会变为
INVISIBLE
是。Is the whole
ListView
turning black? You may need to set:android:cacheColorHint="@android:color/transparent"
on your
ListView
.Otherwise, you may need to provide an alternative to your
INVISIBLE
setting:If you don't do this then recycled views will sometimes be
INVISIBLE
when you don't intend them to be.您需要做的就是在 getView 中将图像设置为可见,然后再次将其设置为不可见/消失(如有必要)。
在我的具体例子中:
All you need to do is set your image to VISIBLE in getView before setting it INVISIBLE/GONE again (if necessary).
In my particular example: