提交 b9a2d3b2 编写于 作者: S Sam Judd

Add method to set a resource id to use with setTag

Fixes #370
上级 ea0a17a7
......@@ -419,6 +419,7 @@ public class Glide {
Request request = target.getRequest();
if (request != null) {
request.clear();
target.setRequest(null);
}
}
......
......@@ -39,10 +39,38 @@ import java.util.List;
*/
public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z> {
private static final String TAG = "ViewTarget";
private static boolean isTagUsedAtLeastOnce = false;
private static Integer tagId = null;
protected final T view;
private final SizeDeterminer sizeDeterminer;
/**
* Sets the android resource id to use in conjunction with {@link View#setTag(int, Object)}
* to store temporary state allowing loads to be automatically cancelled and resources re-used
* in scrolling lists.
*
* <p>
* If no tag id is set, Glide will use {@link View#setTag(Object)}.
* </p>
*
* <p>
* 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 {@link com.bumptech.glide.Glide#clear(View)} on any view
* you start a load into to ensure that the static state is removed.
* </p>
*
* @param tagId The android resource to use.
*/
public static void setTagId(int tagId) {
if (ViewTarget.tagId != null || isTagUsedAtLeastOnce) {
throw new IllegalArgumentException("You cannot set the tag id more than once or change"
+ " the tag id after the first request has been made");
}
ViewTarget.tagId = tagId;
}
public ViewTarget(T view) {
if (view == null) {
throw new NullPointerException("View must not be null!");
......@@ -79,7 +107,7 @@ public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z> {
*/
@Override
public void setRequest(Request request) {
view.setTag(request);
setTag(request);
}
/**
......@@ -96,7 +124,7 @@ public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z> {
*/
@Override
public Request getRequest() {
Object tag = view.getTag();
Object tag = getTag();
Request request = null;
if (tag != null) {
if (tag instanceof Request) {
......@@ -108,6 +136,23 @@ public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z> {
return request;
}
private void setTag(Object tag) {
if (tagId == null) {
isTagUsedAtLeastOnce = true;
view.setTag(tag);
} else {
view.setTag(tagId, tag);
}
}
private Object getTag() {
if (tagId == null) {
return view.getTag();
} else {
return view.getTag(tagId);
}
}
@Override
public String toString() {
return "Target for: " + view;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册