com.bumptech.glide.request.target
Class ViewTarget<T extends View,Z>

java.lang.Object
  extended by com.bumptech.glide.request.target.BaseTarget<Z>
      extended by com.bumptech.glide.request.target.ViewTarget<T,Z>
Type Parameters:
T - The specific subclass of view wrapped by this target.
Z - The resource type this target will receive.
All Implemented Interfaces:
LifecycleListener, Target<Z>
Direct Known Subclasses:
ImageViewTarget

public abstract class ViewTarget<T extends View,Z>
extends BaseTarget<Z>

A base Target for loading Bitmaps into Views that provides default implementations for most most methods and can determine the size of views using a ViewTreeObserver.OnDrawListener.

To detect View reuse in ListView or any ViewGroup that reuses views, this class uses the View.setTag(Object) method to store some metadata so that if a view is reused, any previous loads or resources from previous loads can be cancelled or reused.

Any calls to View.setTag(Object)} on a View given to this class will result in excessive allocations and and/or IllegalArgumentExceptions. If you must call View.setTag(Object) on a view, consider using BaseTarget or SimpleTarget instead.


Field Summary
protected  T view
           
 
Fields inherited from interface com.bumptech.glide.request.target.Target
SIZE_ORIGINAL
 
Constructor Summary
ViewTarget(T view)
           
 
Method Summary
 Request getRequest()
          Returns any stored request using View.getTag().
 void getSize(SizeReadyCallback cb)
          Determines the size of the view by first checking View.getWidth() and View.getHeight().
 T getView()
          Returns the wrapped View.
 void setRequest(Request request)
          Stores the request using View.setTag(Object).
static void setTagId(int tagId)
          Sets the android resource id to use in conjunction with View.setTag(int, Object) to store temporary state allowing loads to be automatically cancelled and resources re-used in scrolling lists.
 String toString()
           
 
Methods inherited from class com.bumptech.glide.request.target.BaseTarget
onDestroy, onLoadCleared, onLoadFailed, onLoadStarted, onStart, onStop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.bumptech.glide.request.target.Target
onResourceReady
 

Field Detail

view

protected final T extends View view
Constructor Detail

ViewTarget

public ViewTarget(T view)
Method Detail

setTagId

public static void setTagId(int tagId)
Sets the android resource id to use in conjunction with View.setTag(int, Object) to store temporary state allowing loads to be automatically cancelled and resources re-used in scrolling lists.

If no tag id is set, Glide will use View.setTag(Object).

Warning: prior to Android 4.0 tags were stored in a static map. Using this method prior to Android 4.0 may cause memory leaks and isn't recommended. If you do use this method on older versions, be sure to call Glide.clear(View) on any view you start a load into to ensure that the static state is removed.

Parameters:
tagId - The android resource to use.

getView

public T getView()
Returns the wrapped View.


getSize

public void getSize(SizeReadyCallback cb)
Determines the size of the view by first checking View.getWidth() and View.getHeight(). If one or both are zero, it then checks the view's ViewGroup.LayoutParams. If one or both of the params width and height are less than or equal to zero, it then adds an ViewTreeObserver.OnPreDrawListener which waits until the view has been measured before calling the callback with the view's drawn width and height.

Parameters:
cb - The callback that must be called when the size of the target has been determined

setRequest

public void setRequest(Request request)
Stores the request using View.setTag(Object).

Specified by:
setRequest in interface Target<Z>
Overrides:
setRequest in class BaseTarget<Z>
Parameters:
request -

getRequest

public Request getRequest()
Returns any stored request using View.getTag().

For Glide to function correctly, Glide must be the only thing that calls View.setTag(Object). If the tag is cleared or set to another object type, Glide will not be able to retrieve and cancel previous loads which will not only prevent Glide from reusing resource, but will also result in incorrect images being loaded and lots of flashing of images in lists. As a result, this will throw an IllegalArgumentException if View.getTag()} returns a non null object that is not an Request.

Specified by:
getRequest in interface Target<Z>
Overrides:
getRequest in class BaseTarget<Z>

toString

public String toString()
Overrides:
toString in class Object