我想将图像加载到适配器中的ImageButton
,这有时不起作用...我有一个带有4个条目的适配器,有时,按钮图像只加载2次而不是4次倍。始终只在第一次创建适配器时......屏幕旋转后左右,一切正常......但在第一次显示时,它无法正常工作......
具有4行的适配器调用2次准备,并且仅在第一次创建时加载两次...
以下是我的适配器getView
:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_link, parent, false);
convertView.setTag(R.id.tag_pos, position);
ImageView iconRow = ViewHolder.get(convertView, R.id.icon);
final ImageButton btOpen = ViewHolder.get(convertView, R.id.btOpen);
// this NEVER fails
PicassoTools.getPicasso().load(item.getIconResId()).into(iconRow);
// this sometimes (at the first start) does not work reliable
PicassoTools.getPicasso().load(isAutoLinked ? R.drawable.linked : R.drawable.unlinked).into(new Target()
{
@Override
public void onPrepareLoad(Drawable d)
{
L.d(this, "BUTTON PREPARE");
}
@Override
public void onBitmapLoaded(Bitmap b, LoadedFrom loadedFrom)
{
L.d(this, "BUTTON LOADED");
btLink.setImageBitmap(b);
}
@Override
public void onBitmapFailed(Drawable d)
{
L.d(this, "BUTTON FAILED");
btLink.setImageBitmap(null);
}
});
return convertView;
}
我的PicassoTools函数(我在这个类中有一些额外的函数):
public static Picasso getPicasso()
{
if (picasso == null)
picasso = new Picasso.Builder(MainApp.getAppContext()).memoryCache(getCache()).build();
return picasso;
}
答案 0 :(得分:4)
使用目标
private Target loadtarget;
将此代码写入getView()
if (loadtarget == null)
loadtarget = new Target() {
@Override
public void onBitmapFailed(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
handleLoadedBitmap(bitmap);
}
@Override
public void onPrepareLoad(Drawable arg0) {
}
};
try {
Picasso.with(this).load(url).into(loadtarget);
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
public void handleLoadedBitmap(Bitmap b) {
BitmapDrawable bdrawable = new BitmapDrawable(b);
imageButton.setBackgroundDrawable(bdrawable);
}
希望这会对你有所帮助:)。