Gintas_
Gintas_

Reputation: 5030

Update RecyclerView item's height after image was loaded?

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:

enter image description here

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:

enter image description here

EDIT: it seems, that onBitmapLoaded() doesn't even get called, although onPrepareLoad() does get.

Upvotes: 2

Views: 1347

Answers (1)

Gintas_
Gintas_

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

Related Questions