毕加索 - 加载图像插入适配器的ImageButton [英] picasso - load image into imagebutton in adapter

查看:493
本文介绍了毕加索 - 加载图像插入适配器的ImageButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在适配器的图像加载到的ImageButton ,这有时不工作...我有4个条目,有时,该按钮适配器图像刚刚加载的2倍,而不是4次。始终只在第一次创作的适配器。经过屏幕旋转左右,一切正常。但在第一显示器上,它不能正常工作...

具有4行的适配器调用2倍prepare和两次装载在第一创建唯一...

以下是我的适配器 getView

  @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
    如果(convertView == NULL)
        convertView = LayoutInflater.from(的getContext())膨胀(R.layout.row_link,父母,假的)。    convertView.setTag(R.id.tag_pos,位置);    ImageView的iconRow = ViewHolder.get(convertView,R.id.icon);
    最终的ImageButton btOpen = ViewHolder.get(convertView,R.id.btOpen);    //这个从来没有失败
    。PicassoTools.getPicasso()负载(item.getIconResId())到(iconRow)。
    //这有时会(在第一次启动)不起作用可靠
    PicassoTools.getPicasso()负载(isAutoLinked R.drawable.linked:R.drawable.unlinked)。.into(新目标()
    {
        @覆盖
        在prepareLoad公共无效(可绘制D)
        {
            L.d(这一点,按钮prePARE);
        }        @覆盖
        公共无效onBitmapLoaded(位图B,LoadedFrom loadedFrom)
        {
            L.d(这一点,按钮装);
            btLink.setImageBitmap(二);
        }        @覆盖
        公共无效onBitmapFailed(可绘制D)
        {
            L.d(这一点,BUTTON失败);
            btLink.setImageBitmap(NULL);
        }
    });    返回convertView;
}

我PicassoTools功能(我在这个类中的一些额外的功能):

 公共静态毕加索getPicasso()
{
    如果(毕加索== NULL)
        。毕加索=新Picasso.Builder(MainApp.getAppContext())的MemoryCache(getCache())建立();
    返回毕加索;
}


解决方案

使用目标

 私人目标loadtarget;

将在getView()这个code

 如果(loadtarget == NULL)
    loadtarget =新目标(){
        @覆盖
        公共无效onBitmapFailed(绘制对象为arg0){
        }        @覆盖
        公共无效onBitmapLoaded(位图位图,从LoadedFrom){
            handleLoadedBitmap(位图);
        }        @覆盖
        在prepareLoad(绘制对象为arg0)公共无效{        }
    };尝试{
    Picasso.with(本).load(URL).into(loadtarget);
}赶上(抛出:IllegalArgumentException IAE){
    iae.printStackTrace();
}公共无效handleLoadedBitmap(位图B){
    BitmapDrawable bdrawable =新BitmapDrawable(B);
    imageButton.setBackgroundDrawable(bdrawable);
}

希望这将帮助您:)

I want to load an image into a ImageButton in an adapter, this is sometimes not working... I have a adapter with 4 entries, and sometimes, the button image is just loaded 2 times instead of 4 times. Always only on the first creation of the adapter... After screen rotation or so, everything works fine... But on the first display, it does not work correctly...

The adapter with 4 rows calls 2 times prepare and two times loaded on the first creation only...

Following is my adapter's 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;
}

My PicassoTools function (I have some extra functions in this class):

public static Picasso getPicasso()
{
    if (picasso == null)
        picasso = new Picasso.Builder(MainApp.getAppContext()).memoryCache(getCache()).build();
    return picasso;
}

解决方案

Use Target

private Target loadtarget;

Write this code in 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);
}

Hope this will help you :)

这篇关于毕加索 - 加载图像插入适配器的ImageButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆