【问题标题】:Picasso Only Loading 5 Images until re-using ones in cachePicasso 仅加载 5 张图像,直到重新使用缓存中的图像
【发布时间】:2015-04-19 05:43:24
【问题描述】:

我对 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()

在此先感谢您!

【问题讨论】:

    标签: android android-layout listview picasso


    【解决方案1】:

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

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

    您只需将return view; 上方的右花括号移动到视图膨胀之后:

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

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

    【讨论】:

    • 哇,简直不敢相信解决方案如此简单。现在工作得很好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多