2015-12-03 45 views
2

我有RecyclerViewLinearLayoutManager。我想要一张图片作为背景,但图片大小是动态的。我希望它是全宽,并有相应的高度。这是我目前有:加载图像后更新RecyclerView项目的高度?

enter image description here

我尝试这样做:

Picasso.with(context).load(pictureUrl).into(new Target() { 
     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) 
     { 
      holder.picture.setImageBitmap(bitmap); 

      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.itemView.getLayoutParams(); 
      params.height = bitmap.getHeight(); 
      holder.itemView.setLayoutParams(params); 
      //notifyItemChanged(position); 
     } 

     @Override public void onBitmapFailed(Drawable drawable) {} 
     @Override public void onPrepareLoad(Drawable drawable) {} 
    }); 

但后来它甚至不加载所有:

enter image description here

编辑:似乎,onBitmapLoaded()甚至没有被调用,虽然onPrepareLoad()确实得到。

+0

你试过adjustViewBounds了吗? –

回答

2

我想我自己设法解决了它。这是我做的:

Picasso.with(context).load(pictureUrl).into(holder.picture, new com.squareup.picasso.Callback() { 
     @Override 
     public void onSuccess() { 
      Drawable drawable = holder.picture.getDrawable(); 
      RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams(); 
      params.height = (int)((float)screenWidth/((float)drawable.getIntrinsicWidth()/(float)drawable.getIntrinsicHeight())); 
      holder.itemView.setLayoutParams(params); 
      holder.itemView.requestLayout(); 
     } 

     @Override 
     public void onError() { 
     } 
    });