【问题标题】:Update RecyclerView item's height after image was loaded?加载图像后更新 RecyclerView 项目的高度?
【发布时间】:2015-12-03 22:10:45
【问题描述】:

我有一个RecyclerViewLinearLayoutManager。我想有一张图片作为背景,但图片大小是动态的。我希望它是全宽并具有相应的高度。这是我目前拥有的:

我试过这个:

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) {}
    });

但是它甚至根本没有加载:

编辑:似乎onBitmapLoaded() 甚至没有被调用,尽管onPrepareLoad() 确实被调用了。

【问题讨论】:

  • 你试过adjustViewBounds吗?

标签: android height android-recyclerview picasso


【解决方案1】:

我想我自己设法解决了这个问题。这是我所做的:

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() {
        }
    });

【讨论】:

    猜你喜欢
    • 2018-12-31
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多