2014-11-03 60 views
1

我想加载图像的ImageButton进入适配器一个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

你能请张贴满适配器代码,并在那里你初始化btLink在getView( )? – 2014-11-03 09:00:01

+0

你真的必须使用毕加索吗?因为我没有使用毕加索我有一个很好的解决方案。 – Harry 2014-11-03 09:28:50

+0

我有一个基于图像的应用程序,因此(因为我仍然得到一些OOM错误)试图在可能到处使用缓存......反正已经给出的答案是解决方案......只是没想到这个小细节了.. – prom85 2014-11-03 09:31:56

回答

3

使用对象

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); 
} 

希望这将帮助你:)

+1

现在你说,我已经读了几个月前...这个目标确实只有一个明确的目标工作...我只是不记得的是,到现在为止...谢谢,我会尝试检查出 – prom85 2014-11-03 09:13:33

+0

肯定:)如果它会帮助请了投票:) – ashutiwari4 2014-11-03 09:16:41

+0

确定......只有一两件事,在适配器我不能做到这一点,你提议的方式......我刚才创建'Target'对象,将其设置为按钮的标签...然后检查该标签以获取相应的“Target”对象... – prom85 2014-11-03 09:29:52