从URL加载RecyclerView.Adapter的图像未更新

时间:2018-11-20 08:58:11

标签: android picasso android-viewholder

我在更改时无法更新RecylcerView.Adapter中的图像。我的适配器拥有此类:

public class ShowMembersAdapter extends RecyclerView.Adapter<ShowMembersAdapter.ViewHolder> {
    private ArrayList<User> users;
    private Context context;
    private ShowTeamsAdapter.OnItemClickListener mListener;

    public void setOnItemClickListener(ShowTeamsAdapter.OnItemClickListener listener) {
        mListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView mImage;

        public ViewHolder(View itemView, ShowTeamsAdapter.OnItemClickListener listener) {
            super(itemView);
            mImage = itemView.findViewById(R.id.image);

            itemView.setOnClickListener(view -> {
                if (listener != null) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(position);
                    }
                }
            });
        }
    }

    public ShowMembersAdapter(ArrayList<User> users, Context context) {
        this.users = users;
        this.context = context;
    }

    @NonNull
    @Override
    public ShowMembersAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_member_item, parent, false);
        ShowMembersAdapter.ViewHolder viewHolder = new ShowMembersAdapter.ViewHolder(v, mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ShowMembers
        String imageUrl = "MYURL" + users.get(position).getEmail() + ".jpg";
        Picasso.get().load(imageUrl).resize(200,200).error(R.mipmap.ic_team_member_no_photo).into(holder.mImage);
    }

    @Override
    public int getItemCount() {
        return users.size();
    }
}

我正在Fragment中填充此类。当我启动一个应用程序时,它运行完美。但是,每当我从应用程序更新图片时,我猜适配器都不会更改,因为图像URL仍然相同。您还有其他我能做的主意吗?您是否认为我应该再打一次Volley请求以获取图片并将其保存到每个用户,所以我不直接从Adapter调用它们吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

更新图片后,您是否尝试过此操作?

const names= [[{name: "John"}, {name: "Mary"}], [{name: "Paul"}, {name: "Peter"}]],
res = names.map(arr => arr.map(elem => ({...elem, country: 'USA'})));

console.log(res);