下面列出了android.view.LayoutInflater#getFactory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Attach a custom Factory interface for creating views while using
* this LayoutInflater. This must not be null, and can only be set once;
* after setting, you can not change the factory.
*
* @see LayoutInflater#setFactory(android.view.LayoutInflater.Factory)
* @deprecated Use {@link #setFactory2(LayoutInflater, LayoutInflater.Factory2)} instead to set
* and {@link LayoutInflater#getFactory2()} to get the factory.
*/
@Deprecated
public static void setFactory(
LayoutInflater inflater, LayoutInflaterFactory factory) {
if (Build.VERSION.SDK_INT >= 21) {
inflater.setFactory2(factory != null ? new LayoutInflaterCompat.Factory2Wrapper(factory) : null);
} else {
final LayoutInflater.Factory2 factory2 = factory != null
? new LayoutInflaterCompat.Factory2Wrapper(factory) : null;
inflater.setFactory2(factory2);
final LayoutInflater.Factory f = inflater.getFactory();
if (f instanceof LayoutInflater.Factory2) {
// The merged factory is now set to getFactory(), but not getFactory2() (pre-v21).
// We will now try and force set the merged factory to mFactory2
forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
} else {
// Else, we will force set the original wrapped Factory2
forceSetFactory2(inflater, factory2);
}
}
}
/**
* Attach a custom {@link LayoutInflater.Factory2} for creating views while using
* this {@link LayoutInflater}. This must not be null, and can only be set once;
* after setting, you can not change the factory.
*
* @see LayoutInflater#setFactory2(android.view.LayoutInflater.Factory2)
*/
public static void setFactory2(
LayoutInflater inflater, LayoutInflater.Factory2 factory) {
inflater.setFactory2(factory);
if (Build.VERSION.SDK_INT < 21) {
final LayoutInflater.Factory f = inflater.getFactory();
if (f instanceof LayoutInflater.Factory2) {
// The merged factory is now set to getFactory(), but not getFactory2() (pre-v21).
// We will now try and force set the merged factory to mFactory2
forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
} else {
// Else, we will force set the original wrapped Factory2
forceSetFactory2(inflater, factory);
}
}
}
static void setFactory(LayoutInflater inflater, PluginFactoryInterface factory) {
if (Build.VERSION.SDK_INT >=11) {
final LayoutInflater.Factory2 factory2 = factory != null
? new FactoryWrapper2(factory) : null;
inflater.setFactory2(factory2);
if (Build.VERSION.SDK_INT < 21) {
final LayoutInflater.Factory f = inflater.getFactory();
if (f instanceof LayoutInflater.Factory2) {
// The merged factory is now set to getFactory(), but not getFactory2() (pre-v21).
// We will now try and force set the merged factory to mFactory2
forceSetFactory2(inflater, (LayoutInflater.Factory2) f);
} else {
// Else, we will force set the original wrapped Factory2
forceSetFactory2(inflater, factory2);
}
}
} else {
final LayoutInflater.Factory factory1 = factory != null
? new FactoryWrapper(factory) : null;
inflater.setFactory(factory1);
}
}
/**
* 初始化
*
* @param original
*/
private void init(LayoutInflater original) {
//将自己设置为LayoutInflaterFactory,接管view的创建
setFactory2(this);
if (mConstructorArgsField != null) {
try {
mConstructorArgs = (Object[]) mConstructorArgsField.get(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
//将自己注册到换肤监听
SkinEngine.registerSkinObserver(this);
if (original == null) {
return;
}
if (original instanceof SkinLayoutInflater) {
SkinLayoutInflater skinLayoutInflater = (SkinLayoutInflater) original;
this.mFactory = skinLayoutInflater.mFactory;
this.mFactory2 = skinLayoutInflater.mFactory2;
} else {
LayoutInflater.Factory factory = original.getFactory();
if (factory instanceof LayoutInflater.Factory2) {
mFactory2 = (Factory2) factory;
} else {
mFactory = factory;
}
}
}
public void installViewFactory() {
LogUtil.d("安装PluginViewFactory");
LayoutInflater layoutInflater = mContext.getLayoutInflater();
if (layoutInflater.getFactory() == null) {
PluginFactoryCompat.setFactory(layoutInflater, this);
} else {
LogUtil.d("The Activity's LayoutInflater already has a Factory installed"
+ " so we can not install plugin's");
}
LogUtil.d("安装PluginViewFactory完成");
}
/**
* Return the current {@link LayoutInflaterFactory} (or null). This is
* called on each element name. If the factory returns a View, add that
* to the hierarchy. If it returns null, proceed to call onCreateView(name).
*
* @return The {@link LayoutInflaterFactory} associated with the
* {@link LayoutInflater}. Will be {@code null} if the inflater does not
* have a {@link LayoutInflaterFactory} but a raw {@link LayoutInflater.Factory}.
* @see LayoutInflater#getFactory()
* @deprecated Use {@link #setFactory2(LayoutInflater, LayoutInflater.Factory2)} to set and
* {@link LayoutInflater#getFactory2()} to get the factory.
*/
@Deprecated
public static LayoutInflaterFactory getFactory(LayoutInflater inflater) {
LayoutInflater.Factory factory = inflater.getFactory();
if (factory instanceof LayoutInflaterCompat.Factory2Wrapper) {
return ((LayoutInflaterCompat.Factory2Wrapper) factory).mDelegateFactory;
}
return null;
}