Glide的clear()方法无法解析。

13

我正在尝试解决我Android应用程序中的OutOfMemoryException问题,在我的recyclerView中,我想写:

@Override
public void onViewRecycled(final ViewHolder viewHolder) {
    Glide.clear(viewHolder.getImageView());
}

但我收到了如下错误:

错误:找不到符号方法clear(ImageView)

我正在使用以下内容:

implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'

25
使用Glide.with(viewholder.itemView.getContext()).clear(viewHolder.getImageView()); - 这可能无法解决OOM问题,但是正确的语法用于清除图像。 - Mark
2
@MarkKeen 你应该将你的评论提交为答案,这样 OP 就可以将其标记为正确答案。 - Demonic218
1个回答

5
根据与`clear`方法相关的文档,请按以下格式进行翻译。
/**
   * Cancel any pending loads Glide may have for the view and free any resources that may have been
   * loaded for the view.
   *
   * <p> Note that this will only work if {@link View#setTag(Object)} is not called on this view
   * outside of Glide. </p>
   *
   * @param view The view to cancel loads and free resources for.
   * @throws IllegalArgumentException if an object other than Glide's metadata is put as the view's
   *                                  tag.
   * @see #clear(Target)
   */
  public void clear(@NonNull View view) {
    clear(new ClearTarget(view));
  }

因此,您的OutOfMemoryException将由清理方法处理。

并且稍微更改您的代码,将上下文传递给Glide:

Glide.with(viewHolder.getImageView().getContext()).clear(viewHolder.getImageView());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接