gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:05:31 25 4
gpt4 key购买 nike

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

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() 确实被调用。

最佳答案

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

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

关于android - 加载图像后更新 RecyclerView 项目的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com