【发布时间】:2020-05-29 14:00:57
【问题描述】:
我通过 Retrofit 将图像发送到服务器。发送过程完成后,我的 FeedFragment 正在打开,最后发送的图像必须显示在 RecyclerView 的顶部(第一个位置)。但是 imageView 在第一次加载时是不可见的。只有当我向下滚动并向上滚动时,它才会变得可见。 Glide 代码(在 RecyclerView 适配器中):
Glide.with(context)
.asBitmap()
.load(ApiClient.Base_URL + imagesList.get(0).getImage())
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.dontAnimate()
.dontTransform()
.thumbnail(0.1f)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
progressBar.setVisibility(View.GONE);
//for dynamic image height:
targetImageHeight = (resource.getHeight() * deviceHeight) / resource.getWidth();
if (targetImageHeight >= deviceHeight) {
targetImageHeight = deviceHeight;
}
imageView.getLayoutParams().height = targetImageHeight;
imageView.setImageBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
Glide.with(context).clear(imageView);
progressBar.setVisibility(View.GONE);
}
});
xml:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<uk.co.senab.photoview.PhotoView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="400dp" />
</RelativeLayout>
【问题讨论】:
-
问题是视图还没有绘制。正确的方法是使用 view.post() 方法,该方法仅在视图完成绘制后才会执行提供的可运行文件
-
@Heisenberg 我无法完全理解。能否举个例子(代码)Mr White?
标签: android android-recyclerview android-glide