Android 消失图像列表视图

Android 消失图像列表视图,android,listview,android-arrayadapter,android-listfragment,Android,Listview,Android Arrayadapter,Android Listfragment,我对列表视图有问题 在我的应用程序中有一个ListFragment,其中包含一个通知列表,基本上是一个文本和一个图像 单击时,它们也可能被禁用。单击后,我将structNotification的字段“active”设置为false,并调用NotifyDataSetChanged方法。但当我上下滚动时,一些图像会消失,并可能再次出现。但当我不滚动它的时候 所有图像都在参考资料中,我从未设置列表项的可见性 以下是我的一些代码: 定制阵列适配器 public class NotificationsAr

我对列表视图有问题

在我的应用程序中有一个
ListFragment
,其中包含一个通知列表,基本上是一个文本和一个图像

单击时,它们也可能被禁用。单击后,我将struct
Notification
的字段“active”设置为false,并调用
NotifyDataSetChanged
方法。但当我上下滚动时,一些图像会消失,并可能再次出现。但当我不滚动它的时候

所有图像都在参考资料中,我从未设置列表项的可见性

以下是我的一些代码:

定制阵列适配器

public class NotificationsArrayAdapter extends ArrayAdapter<Notification> {
    private final Context context;
    private final String[] message_strings;
    private final Drawable[] images;
    private final LayoutInflater layoutInflater;
    private ArrayList<Notification> items;

    public NotificationsArrayAdapter (Context context, Resources resources, ArrayList<Notification> items) {
    super(context, R.layout.customer_notifications_list_item, items);
    this.context = context;
    this.items = items;
    this.message_strings = new String[] {"message1", "message2", "message3", "message4"};

    this.images = new Drawable[] {ContextCompat.getDrawable(context, R.drawable.notifications_blue_circle),
                ContextCompat.getDrawable(context, R.drawable.notifications_red_circle),
                ContextCompat.getDrawable(context, R.drawable.notifications_grey_circle),
                ContextCompat.getDrawable(context, R.drawable.notifications_green_circle)
        };

        this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            v = layoutInflater.inflate(R.layout.customer_notifications_list_item, parent, false);
        }

        Notification item = getItem(position);

        if (item != null) {
            TextView textView = (TextView) v.findViewById(R.id.notifications_list_item_text);
            ImageView imageView = (ImageView) v.findViewById(R.id.notifications_list_item_image);

            if (textView != null) {
                if (item.getActive()) {
                    textView.setTextColor(Color.parseColor("#FFFFFF"));
                }
                else {
                    textView.setTextColor(Color.parseColor("#BBBBBB"));
                }
                textView.setText(message_strings[item.getType()]);
            }

            if (imageView != null) {
                if (item.getActive()) {
                    imageView.setImageDrawable(images[item.getType()]);
                }
                else {
                    imageView.setImageAlpha(0);
                }
            }
        }

    return v;
    }
}
公共类通知数组适配器扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]消息字符串;
私人最终可绘制[]图像;
私人最终裁员;
私有ArrayList项;
public NotificationsArrayAdapter(上下文上下文、资源、ArrayList项){
超级(上下文、右布局、客户通知、列表、项目);
this.context=上下文;
这个项目=项目;
this.message_strings=新字符串[]{“message1”、“message2”、“message3”、“message4”};
this.images=newdrawable[]{ContextCompat.getDrawable(context,R.Drawable.notifications_blue_circle),
ContextCompat.getDrawable(context,R.drawable.notifications\u red\u circle),
ContextCompat.getDrawable(context,R.drawable.notifications\u grey\u circle),
ContextCompat.getDrawable(context,R.drawable.notifications\u green\u circle)
};
this.layoutInflater=(layoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
v=布局更平坦。充气(R.layout.customer\u notifications\u list\u item,parent,false);
}
通知项=获取项(位置);
如果(项!=null){
TextView TextView=(TextView)v.findViewById(R.id.notifications\u list\u item\u text);
ImageView ImageView=(ImageView)v.findViewById(R.id.notifications\u list\u item\u image);
if(textView!=null){
if(item.getActive()){
textView.setTextColor(Color.parseColor(#FFFFFF”);
}
否则{
textView.setTextColor(Color.parseColor(#BBBBBB”);
}
textView.setText(消息字符串[item.getType()]);
}
如果(imageView!=null){
if(item.getActive()){
setImageDrawable(图像[item.getType()]);
}
否则{
imageView.setImageAlpha(0);
}
}
}
返回v;
}
}
片段:

public class CustomerNotificationsFragment extends ListFragment {

    public CustomerNotificationsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_customer_notifications, container, false);

        ArrayList<Notification> notifications = new ArrayList<Notification>();
        for (int i = 0; i < 20; ++i) {
            notifications.add(new Notification(1, "blabal"));
        }

        NotificationsArrayAdapter notificationsArrayAdapter = new NotificationsArrayAdapter(getActivity(), getResources(), notifications);
        setListAdapter(notificationsArrayAdapter);

        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Notification item = (Notification) this.getListAdapter().getItem(position);
        item.setActive(false);

        ((ArrayAdapter) getListAdapter()).notifyDataSetChanged();
    }
}
公共类CustomerNotificationsFragment扩展了ListFragment{
公共CustomerNotificationsFragment(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u customer\u通知,容器,false);
ArrayList通知=新建ArrayList();
对于(int i=0;i<20;++i){
通知。添加(新通知(1,“blabal”);
}
NotificationsArrayAdapter NotificationsArrayAdapter=新建NotificationsArrayAdapter(getActivity(),getResources(),notifications);
setListAdapter(notificationsArrayAdapter);
返回视图;
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
通知项=(通知)this.getListAdapter().getItem(位置);
item.setActive(假);
((ArrayAdapter)getListAdapter()).notifyDataSetChanged();
}
}
这可能就是你的问题所在。我建议在上设置一个断点

imageView.setImageAlpha(0);
查看是否在图像消失前触发


另外,我假设您的通知对象不是Android对象?我在文档中找不到getActive()方法。这个方法的作用是什么?

listview会回收视图,所以我发现以前我做过类似的事情,我会更改数据,滚动后会将列表更改回原始或其他奇怪的内容。为了解决这个问题,我使用了

自定义适配器中的getView将如下所示

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if(convertView == null){

        convertView = LayoutInflater.from(context).inflate(R.layout.customer_notifications_list_item, parent, false);

        viewHolder = new ViewHolder();

        viewHolder.textView = (TextView) convertView.findViewById(R.id.notifications_list_item_text);
        viewHolder.imageView = (TextView) convertView.findViewById(R.id.notifications_list_item_image);

        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Notification item = getItem(position);

    if (item != null) {

        if (viewHolder.textView != null) {
            if (item.getActive()) {
                viewHolder.textView.setTextColor(Color.parseColor("#FFFFFF"));
            }
            else {
                viewHolder.textView.setTextColor(Color.parseColor("#BBBBBB"));
            }
            viewHolder.textView.setText(message_strings[item.getType()]);
        }

        if (viewHolder.imageView != null) {
            if (item.getActive()) {
                viewHolder.imageView.setImageDrawable(images[item.getType()]);
            }
            else {
                viewHolder.imageView.setImageAlpha(0);
            }
        }
    }

    return convertView;
}

static class ViewHolder {
    protected TextView textView;
    protected ImageView imageView;

}

适配器中的视图是循环使用的。这意味着您将结束使用imageAlpha=0的项,即使它们满足此处的if语句:

if (viewHolder.imageView != null) {
    if (item.getActive()) {
        viewHolder.imageView.setImageDrawable(images[item.getType()]);
    } 
    else {
       viewHolder.imageView.setImageAlpha(0);
    }
}
由于循环,您应该更改语句两部分中的关键属性。所以,应该是这样的:

if (viewHolder.imageView != null) {
      if (item.getActive()) {
          viewHolder.imageView.setImageAlpha(255);
          viewHolder.imageView.setImageDrawable(images[item.getType()]);
      }
      else {
          viewHolder.imageView.setImageAlpha(0);
      }
}
以下是列表项回收工作原理的图像:

资料来源:(它还有一个关于LisView的教程)

我还建议使用viewHolder方法(更快、更高效),这里有一个googletalk(),您也可以使用提供的代码


希望有帮助:)

通知是我自己的类,它只包含一些值。Active是一个布尔常量,其中getAlpha()是getter。setAlpha仅对单击的项触发。通过日志检查。谢谢,这很有帮助,但还有一件事:当我检查一个项目并开始滚动时,只有一个图像开始出现和消失,所有其他图像都正常。只有当列表位于特定位置下方时,它才会消失。你知道可能是什么情况吗?你的答案仍然有一个错误:setImageAlpha在0到255的范围内,所以应该用参数255调用它。如果有人有同样的问题,他根本看不到任何图片。在setImageAlpha(255)之后,图像仍然消失?(对不起,有点搞砸了
if (viewHolder.imageView != null) {
      if (item.getActive()) {
          viewHolder.imageView.setImageAlpha(255);
          viewHolder.imageView.setImageDrawable(images[item.getType()]);
      }
      else {
          viewHolder.imageView.setImageAlpha(0);
      }
}