Fresco image loading callback

你。 提交于 2019-12-01 20:27:30
Snehal

Can hide on onFailure method:

ControllerListener listener = new BaseControllerListener<ImageInfo>() {

                @Override
                public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
                   //Action on final image load
                }
                @Override
                public void onFailure(String id, Throwable throwable) {
                    //Action on failure
                }

            };
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(uri)
                    .setControllerListener(listener)
                    .build();
            draweeView.setController(controller);

Regarding 1, perhaps you can do something like this:

class ControllerListenerWithView() extends BaseControllerListener {
   private final WeakReference<View> mViewReference;

   ControllerListenerWithView(View view) {
     mViewReference = new WeakReference<>(view);
   }

   @Nullable
   protected View getView() {
     return mViewReference.get();
   }
}

Then:

ControllerListener controllerListener = new ControllerListenerWithView(holder.simpleDraweeViewImage) {
  @Override
  public void onFailure(String id, Throwable throwable) {
    View view = getView();
    if (view != null) {
      view.setVisibility(View.GONE);
    }
  }
};

If you don't have the view accessible at the listener creation time, instead of passing view via listener constructor, you can add a setter method and do:

controllerListener.setView(holder.simpleDraweeViewImage);
controller = ...
holder.simpleDraweeViewImage.setController(controller);

If this looks ugly to you, well, that's because it is ugly :) Design that involves circular references is just ugly. DraweeController doesn't have a reference to the view (not directly at least). DraweeController references a DraweeHierarchy which references Drawables and the top-level drawable has a WeakReference to the parent view in order to propagate Drawable.Callback events. But that's it. DraweeController doesn't need view and we can't/won't keep reference to the view in it. The reason for that is that DraweeControllers and DraweeHierarchies can be used in contexts other than View and it is unnecessary for controller to have a back reference to the view. DraweeController controls DraweeHierarchy, not the view.

Regarding 2, while building controller, you can specify setOldController(view.getController()). That way the old controller which you are replacing will be reused while building a new one. This saves allocations and helps scroll-perf.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!