简体   繁体   English

如何修复:“notifyDataSetChanged() 未更新 RecyclerView Adapter 中的 ImageView”

[英]How to fix:'notifyDataSetChanged() not updating the ImageView in RecyclerView Adapter'

Suppose I have a recyclerView Adapter which is used to populate views including imageView .假设我有一个 recyclerView 适配器,用于填充包括 imageView 在内的视图。 When i first load adapter then recycler view items are getting displayed properly .当我第一次加载适配器时,回收站视图项目会正确显示。 But if i update imageView and textview resource in adapter datasource and call notifyDataSetChanged() method then only textview is getting updated and not imageView .但是如果我更新适配器数据源中的 imageView 和 textview 资源并调用 notifyDataSetChanged() 方法,那么只有 textview 得到更新,而不是 imageView 。 Can anyone help me with this ?谁能帮我这个 ? Thanks in advance .提前致谢 。

For Example - This is the first time i add all the elements in an empty arraylist inside fragment and update using notifyDataSetChanged() method .例如 - 这是我第一次将所有元素添加到片段内的空数组列表中,并使用 notifyDataSetChanged() 方法进行更新。 Till now everything is fine and images are loading correctly in adapter .直到现在一切都很好,图像在适配器中正确加载。

Below is the getMethod()下面是 getMethod()

                    if(remindersListResponse.code == 200)
                    {
                        if(remindersListResponse.data != null)
                        {
                            photoContactList.clear();

                           photoContactList.addAll(remindersListResponse.data);
                            photoContactsAdapter.notifyDataSetChanged();
                        }

                    }

Now if i call getMethod again on click of some button in fragment and use notifyDataSetChanged() method .现在,如果我点击片段中的某个按钮再次调用 getMethod 并使用 notifyDataSetChanged() 方法。 Then only text gets updated in adapter items but not images instead they get disappear .然后只有文本在适配器项目中更新,而不是图像而不是它们消失。

This is my adapter onBindViewHolder(...) method这是我的适配器 onBindViewHolder(...) 方法

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {

        final PhotoContactsResponse.Result photoContactDetail = photoContactList.get(i);

        viewHolder.photoContactName.setText(photoContactDetail.name);

        if(photoContactDetail.image!=null && !photoContactDetail.image.isEmpty()) {
            viewHolder.addPhotoText.setVisibility(View.GONE);
            Picasso.with(context)
                    .load(Constants.WEB_SERVICE_BASE_URL_FOR_IMAGE+photoContactDetail.image)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(viewHolder.photoContactImage);
        }
        else
        {
            viewHolder.addPhotoText.setVisibility(View.VISIBLE);
            viewHolder.photoContactImage.setVisibility(View.GONE);
        }
}

This is my model class这是我的模型课

public static class Result{
        public int id;
        public String name;
        public String number;
        public String image;
        public int index;
    }

I was running into this same issue and fixed it by setting the recycle view's adapter to null, then to my adapter again.我遇到了同样的问题,并通过将回收视图的适配器设置为 null,然后再次设置为我的适配器来修复它。 See: Force RecyclerView to redraw its items请参阅: 强制 RecyclerView 重绘其项目

I think you forgot to set ImageView as VISIBLE.我认为您忘记将 ImageView 设置为 VISIBLE。 When you hide it in else statement you should always bring it back when image is available.当您将它隐藏在else语句中时,您应该始终在图像可用时将其带回来。 onBindViewHolder only binds data to view. onBindViewHolder只将数据绑定到视图。 Recycler views are reused between calls to onBindViewHolder method.在对onBindViewHolder方法的调用之间重用回收器视图。

@Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {

        final PhotoContactsResponse.Result photoContactDetail = photoContactList.get(i);

        viewHolder.photoContactName.setText(photoContactDetail.name);

        if(photoContactDetail.image!=null && !photoContactDetail.image.isEmpty()) {
            viewHolder.addPhotoText.setVisibility(View.GONE); 
            viewHolder.photoContactImage.setVisibility(View.VISIBLE); // <== ADD THIS LINE

            Picasso.with(context)
                    .load(Constants.WEB_SERVICE_BASE_URL_FOR_IMAGE+photoContactDetail.image)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(viewHolder.photoContactImage);
        }
        else
        {
            viewHolder.addPhotoText.setVisibility(View.VISIBLE);
            viewHolder.photoContactImage.setVisibility(View.GONE);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 notifyDataSetChanged() 不更新 RecyclerView 适配器 - notifyDataSetChanged() not updating RecyclerView Adapter 使用adapter.notifyDataSetChanged()更新RecyclerView - Updating RecyclerView with adapter.notifyDataSetChanged() Recyclerview适配器的notifyDataSetChanged不更新RecyclerView - Recyclerview Adapter's notifyDataSetChanged not updating RecyclerView 在 Recyclerview 适配器上设置 notifyDataSetChanged() - Set notifyDataSetChanged() on Recyclerview adapter setOnClickListener RecyclerView 适配器中的 notifyDataSetChanged - notifyDataSetChanged in setOnClickListener RecyclerView Adapter RecyclerView适配器notifyDataSetChanged不起作用 - RecyclerView Adapter notifyDataSetChanged not working recyclerview适配器notifydatasetchanged不起作用 - recyclerview adapter notifydatasetchanged not work Firebase Ui Paging 适配器的 notifyDataSetChanged 未更新 RecyclerView - Firebase Ui Paging adapter's notifyDataSetChanged not updating the RecyclerView notifydatasetchanged后recyclerview不更新 - recyclerview not updating after notifydatasetchanged notifyDataSetChanged()在自定义RecyclerView适配器上不起作用 - notifyDataSetChanged() not working on custom RecyclerView adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM