毕加索只加载 5 张图像,直到在缓存中重新使用这些图像

Picasso Only Loading 5 Images until re-using ones in cache

我对 android 编程还很陌生。我 运行 在将图像(位图)加载到 ListView 时遇到了一些问题。它可以完美地工作,直到我加载了大约 5 张图像,然后它重新使用我加载的第一张图像(假设来自缓存)。我已经尝试了很多解决方案和位图采样,但仍然没有运气。这是我的代码:

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        if (view == null) {

            view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);

            Contact currentContact = Contacts.get(position);
            TextView name = (TextView) view.findViewById(R.id.contactName);

            name.setText(currentContact.getName());
            TextView phone = (TextView) view.findViewById(R.id.phoneNumber);
            phone.setText(currentContact.getPhone());
            TextView email = (TextView) view.findViewById(R.id.email);
            email.setText(currentContact.getEmail());
            TextView address = (TextView) view.findViewById(R.id.cAddress);
            address.setText(currentContact.getAddress());

            Context mContext = getApplicationContext();

            File file = mContext.getDir("imageDir", Context.MODE_PRIVATE);

            String path = "file:" + file.getPath() + "/" + currentContact.getImage() + ".png";

            ImageView contactListImage = (ImageView) view.findViewById(R.id.contactListImage);

              Picasso.with(getApplicationContext())      
                        .load(path)
                        .centerInside()
                        .fit()
                        .error(R.drawable.user_2)
                        .into(contactListImage)
        }
        return view;
    }

我确实尝试过使用

recycleMemoryCache()

提前致谢!

您没有正确使用回收视图。

你的 view 参数要么是 null,这意味着你必须膨胀一个新的,要么是 not null,这意味着你应该 重新使用它 - 为所有字段等分配新值

您只需将 return view; 上方的结束花括号移动到您的视图 inflation:

之后
if (view == null) {
    view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);
}

顺便说一句,我会查找 viewholder 模式 - 或者只是开始使用 RecyclerView,因为它会强制执行(并且是 Android 上列表的未来方式)。