am c44430a3: Merge "Tools attribute improvements for RecyclerView LayoutManager." into lmp-mr1-dev

* commit 'c44430a36813dc4afb6000279254e578bc5d0391':
  Tools attribute improvements for RecyclerView LayoutManager.
This commit is contained in:
Deepanshu Gupta
2015-03-12 01:45:25 +00:00
committed by Android Git Automerger
3 changed files with 36 additions and 21 deletions

View File

@@ -233,11 +233,13 @@ public final class BridgeInflater extends LayoutInflater {
String type = attrs.getAttributeValue(BridgeConstants.NS_RESOURCES, String type = attrs.getAttributeValue(BridgeConstants.NS_RESOURCES,
BridgeConstants.ATTR_LAYOUT_MANAGER_TYPE); BridgeConstants.ATTR_LAYOUT_MANAGER_TYPE);
if (type != null) { if (type != null) {
LayoutManagerType layoutManagerType = LayoutManagerType.getByDisplayName(type); LayoutManagerType layoutManagerType = LayoutManagerType.getByLogicalName(type);
if (layoutManagerType == null) { if (layoutManagerType == null) {
Bridge.getLog().warning(LayoutLog.TAG_UNSUPPORTED, layoutManagerType = LayoutManagerType.getByClassName(type);
"LayoutManager (" + type + ") not found, falling back to " + }
"LinearLayoutManager", null); if (layoutManagerType == null) {
// add the classname itself.
bc.addCookie(view, type);
} else { } else {
bc.addCookie(view, layoutManagerType); bc.addCookie(view, layoutManagerType);
} }

View File

@@ -50,5 +50,5 @@ public class BridgeConstants {
public final static String WRAP_CONTENT = "wrap_content"; public final static String WRAP_CONTENT = "wrap_content";
/** Attribute in the tools namespace used to specify layout manager for RecyclerView. */ /** Attribute in the tools namespace used to specify layout manager for RecyclerView. */
public static final String ATTR_LAYOUT_MANAGER_TYPE = "layoutManagerType"; public static final String ATTR_LAYOUT_MANAGER_TYPE = "layoutManager";
} }

View File

@@ -73,8 +73,15 @@ public class RecyclerViewUtil {
private static void setLayoutManager(@NonNull View recyclerView, @NonNull BridgeContext context, private static void setLayoutManager(@NonNull View recyclerView, @NonNull BridgeContext context,
@NonNull IProjectCallback callback) throws ReflectionException { @NonNull IProjectCallback callback) throws ReflectionException {
Object cookie = context.getCookie(recyclerView); Object cookie = context.getCookie(recyclerView);
assert cookie == null || cookie instanceof LayoutManagerType; assert cookie == null || cookie instanceof LayoutManagerType || cookie instanceof String;
if (cookie == null) { if (!(cookie instanceof LayoutManagerType)) {
if (cookie != null) {
// TODO: When layoutlib API is updated, try to load the class with a null
// constructor or a constructor taking one argument - the context.
Bridge.getLog().warning(LayoutLog.TAG_UNSUPPORTED,
"LayoutManager (" + cookie + ") not found, falling back to " +
"LinearLayoutManager", null);
}
cookie = LayoutManagerType.getDefault(); cookie = LayoutManagerType.getDefault();
} }
Object layoutManager = createLayoutManager((LayoutManagerType) cookie, context, callback); Object layoutManager = createLayoutManager((LayoutManagerType) cookie, context, callback);
@@ -152,22 +159,13 @@ public class RecyclerViewUtil {
"android.support.v7.widget.StaggeredGridLayoutManager", "android.support.v7.widget.StaggeredGridLayoutManager",
new Class[]{int.class, int.class}, new Object[]{2, LinearLayout.VERTICAL}); new Class[]{int.class, int.class}, new Object[]{2, LinearLayout.VERTICAL});
private String mDisplayName; private String mLogicalName;
private String mClassName; private String mClassName;
private Class[] mSignature; private Class[] mSignature;
private Object[] mArgs; private Object[] mArgs;
private static final HashMap<String, LayoutManagerType> sDisplayNameLookup = LayoutManagerType(String logicalName, String className, Class[] signature, Object[] args) {
new HashMap<String, LayoutManagerType>(); mLogicalName = logicalName;
static {
for (LayoutManagerType type : LayoutManagerType.values()) {
sDisplayNameLookup.put(type.mDisplayName, type);
}
}
LayoutManagerType(String displayName, String className, Class[] signature, Object[] args) {
mDisplayName = displayName;
mClassName = className; mClassName = className;
mSignature = signature; mSignature = signature;
mArgs = args; mArgs = args;
@@ -199,8 +197,23 @@ public class RecyclerViewUtil {
} }
@Nullable @Nullable
public static LayoutManagerType getByDisplayName(@Nullable String className) { public static LayoutManagerType getByLogicalName(@NonNull String logicalName) {
return sDisplayNameLookup.get(className); for (LayoutManagerType type : values()) {
if (logicalName.equals(type.mLogicalName)) {
return type;
}
}
return null;
}
@Nullable
public static LayoutManagerType getByClassName(@NonNull String className) {
for (LayoutManagerType type : values()) {
if (className.equals(type.mClassName)) {
return type;
}
}
return null;
} }
} }
} }