列表项中的 Android 列表视图图像视图在滚动时隐藏

发布于 2024-10-27 15:22:12 字数 1979 浏览 1 评论 0原文

当尝试动态设置列表项中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-11-03 15:22:12

整个ListView变黑了吗?您可能需要

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

ListView

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

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

如果您不这样做,那么当您不希望回收的视图这样做时,它们有时会变为 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 (name.equalsIgnoreCase("Add Alarm")) {
    holder.ind.setVisibility(View.INVISIBLE);
} else {
    holder.ind.setVisibility(View.VISIBLE);
}

If you don't do this then recycled views will sometimes be INVISIBLE when you don't intend them to be.

七秒鱼° 2024-11-03 15:22:12

您需要做的就是在 getView 中将图像设置为可见,然后再次将其设置为不可见/消失(如有必要)。

在我的具体例子中:

    holder.ivImageLeft.setVisibility(View.VISIBLE);
    holder.ivImageRight.setVisibility(View.VISIBLE);

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

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:

    holder.ivImageLeft.setVisibility(View.VISIBLE);
    holder.ivImageRight.setVisibility(View.VISIBLE);

    if (i == 0) {                       
        holder.ivImageLeft.setVisibility(View.GONE);
    } else {                                               
        holder.ivImageRight.setVisibility(View.GONE);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文