加载图像后更新RecyclerView项目的高度?

时间:2015-12-03 13:36:12

标签: android height android-recyclerview picasso

我有RecyclerView LinearLayoutManager。我希望将图片作为背景,但图片大小是动态的。我希望它是全宽度并相应地具有高度。这就是我目前所拥有的:

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()确实得到了。

1 个答案:

答案 0 :(得分: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() {
        }
    });