首页 文章

listitem中的Android listview imageview在滚动时隐藏

提问于
浏览
2

在尝试动态设置列表项中的 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 回答

  • 2

    整个_849394变黑了吗?您可能需要设置:

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

    ListView .

    否则,您可能需要提供 INVISIBLE 设置的替代方法:

    if (name.equalsIgnoreCase("Add Alarm")) {
        holder.ind.setVisibility(View.INVISIBLE);
    } else {
        holder.ind.setVisibility(View.VISIBLE);
    }
    

    如果您不这样做,那么当您不打算使用时,回收的视图有时会是 INVISIBLE .

  • 2

    您需要做的就是在getView中将图像设置为VISIBLE,然后再将其设置为INVISIBLE / GONE(如有必要) .

    在我的特定示例中:

    holder.ivImageLeft.setVisibility(View.VISIBLE);
        holder.ivImageRight.setVisibility(View.VISIBLE);
    
        if (i == 0) {                       
            holder.ivImageLeft.setVisibility(View.GONE);
        } else {                                               
            holder.ivImageRight.setVisibility(View.GONE);
        }
    

相关问题