Reputation: 5030
I have a RecyclerView
with LinearLayoutManager
. I want to have a picture as a background, but picture size is dynamic. I want it to be full width and have height accordingly. This is what I currently have:
I tried this:
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) {}
});
But then it doesn't even load at all:
EDIT: it seems, that onBitmapLoaded()
doesn't even get called, although onPrepareLoad()
does get.
Upvotes: 2
Views: 1347
Reputation: 5030
I think I managed to solve it myself. Here is what I did:
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() {
}
});
Upvotes: 2