其他分享
首页 > 其他分享> > Android在ListView中使用Picasso和TextDrawable

Android在ListView中使用Picasso和TextDrawable

作者:互联网

我有一个用户图像的ListView.一些用户有个人资料图片,我使用Picasso加载图像.
如果用户没有图像,则使用TextDrawable如下图所示在我的列表视图适配器中设置其图像.

if (picture!=null) {
         Uri imageUri = Uri.parse(picture);
         Picasso.with(holder.pic.getContext()).load(imageUri.toString()).fit().centerCrop().into(holder.pic);

} else {
        ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
        int color = generator.getColor(userName);
        String userChar = userName.substring(0, 1);
        TextDrawable drawable = TextDrawable.builder()
                .buildRound(userChar.toUpperCase(), color);
        holder.pic.setImageDrawable(drawable);

    }

问题是,当我滚动列表时,有时图像会弄乱,那些没有图像且应该与TextDrawable一起显示的图像会与其他用户的个人资料图片一起显示.
如果我使用Picasso加载所有图像,则不会发生.无论如何,是否可以将TextDrawable对象传递到毕加索以解决此问题?还是有其他解决方案?

解决方法:

您可以将TextDrawable用作Picasso的可绘制占位符.然后,我认为您的问题将得到解决.

ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color = generator.getColor(userName);
String userChar = userName.substring(0, 1);
TextDrawable drawable = TextDrawable.builder()
            .buildRound(userChar.toUpperCase(), color);
Picasso.with(holder.pic.getContext()).
    load(imageUri.toString()).
    placeholder(drawable).
    error(drawable).
    centerCrop().
    into(holder.pic);

标签:picasso,android
来源: https://codeday.me/bug/20191027/1945099.html