2015-12-03 80 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() { 
     } 
    });