LayoutInflater.Factory 22.1+ 支持库不调用 onCreateView
LayoutInflater.Factory onCreateView is not called with 22.1+ support library
我的 LayoutInflater.Factory
(下面的代码示例)调用 onCreateView
并与 'com.android.support:support-v4:22.0.0' 一起正常工作的主要问题。但是当我移动到 'com.android.support:support-v4:22.1.0' 或更高时,不会调用 onCreateView
。我不明白为什么?
//From many fragments i Call hintManager.inflate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
layout = hintManager.inflate(inflater, R.layout.generate_code);
...
//here is HintManager method called from various fragments.
public View inflate(LayoutInflater inflater, int layoutResourceId) {
AttributeParser attributeParser = new AttributeParser();
LayoutInflater layoutInflater = attributeParser.getLayoutInflater(inflater);
final View v = layoutInflater.inflate(layoutResourceId, null);
//here AttributeParserFactory#onCreateView should be called, but it fails with 22.1+ support lib, but works with 22.0
attributeParser.setViewAttribute(v);
return v;
}
...
//example of factory, that works fine with 22.0 support lib
private Map<Integer, HashMap<Integer, String>> helpViewList;
private class AttributeParser {
private AttributeParserFactory mFactory;
private class AttributeParserFactory implements LayoutInflater.Factory{
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
String id = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "id");
if(id != null){
// String with the reference character "@", so we strip it to keep only the reference
id = id.replace("@", "");
TypedArray libraryStyledAttributeList = context.obtainStyledAttributes(attrs, R.styleable.Help);
HashMap<Integer, String> libraryViewAttribute = new HashMap<Integer, String>();
int i = 0;
for(int attribute : R.styleable.Help){
String attributeValue = null;
if (attribute == R.styleable.Help_arrow || attribute == R.styleable.Help_padding) {
attributeValue = String.valueOf(libraryStyledAttributeList.getInteger(i, 0));
}
else {
attributeValue = libraryStyledAttributeList.getString(i);
}
if(attributeValue != null)
libraryViewAttribute.put(attribute, attributeValue);
i++;
}
if(!libraryViewAttribute.isEmpty()) {
helpViewList.put(Integer.valueOf(id), libraryViewAttribute);
}
libraryStyledAttributeList.recycle();
}
return null;
}
}
public AttributeParser(){
mFactory = new AttributeParserFactory();
}
public void clear() {
helpViewList.clear();
}
public LayoutInflater getLayoutInflater(LayoutInflater inflater) {
LayoutInflater layoutInflater = inflater.cloneInContext(inflater.getContext());
layoutInflater.setFactory(mFactory);
return layoutInflater;
}
public void setFactory(LayoutInflater inflater){
inflater.cloneInContext(inflater.getContext()).setFactory(mFactory);
}
public void setViewAttribute(Activity activity) {
for(Map.Entry<Integer, HashMap<Integer, String>> attribute : helpViewList.entrySet())
if(activity.findViewById((Integer) attribute.getKey()) != null)
activity.findViewById((Integer) attribute.getKey()).setTag(attribute.getValue());
}
public void setViewAttribute(View view) {
for(Map.Entry<Integer, HashMap<Integer, String>> attribute : helpViewList.entrySet())
if(view.findViewById((Integer) attribute.getKey()) != null)
view.findViewById((Integer) attribute.getKey()).setTag(attribute.getValue());
}
public Map<Integer, HashMap<Integer, String>> getAttributeList() {
return helpViewList;
}
public void setAttributeList(Map<Integer, HashMap<Integer, String>> attributeList) {
helpViewList = attributeList;
}
}
编辑后的答案:
为了解决这个问题,我们应该使用 LayoutInflaterCompat.setFactory 来帮助 pre/post Lollipop 中的 LayoutInflater Factory。
这是例子
LayoutInflater layoutInflaterCopy = inflater.cloneInContext(inflater.getContext());
LayoutInflaterCompat.setFactory(layoutInflaterCopy, new LayoutInflaterFactory() {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
String id = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "id");
if(id != null){
// String with the reference character "@", so we strip it to keep only the reference
id = id.replace("@", "");
TypedArray libraryStyledAttributeList = context.obtainStyledAttributes(attrs, R.styleable.Help);
HashMap<Integer, String> libraryViewAttribute = new HashMap<Integer, String>();
int i = 0;
for(int attribute : R.styleable.Help){
String attributeValue = null;
if (attribute == R.styleable.Help_arrow || attribute == R.styleable.Help_padding) {
attributeValue = String.valueOf(libraryStyledAttributeList.getInteger(i, 0));
}
else {
attributeValue = libraryStyledAttributeList.getString(i);
}
if(attributeValue != null)
libraryViewAttribute.put(attribute, attributeValue);
i++;
}
if(!libraryViewAttribute.isEmpty()) {
helpViewList.put(Integer.valueOf(id), libraryViewAttribute);
}
libraryStyledAttributeList.recycle();
}
return null;
}
});
final View v = layoutInflaterCopy.inflate(layoutResourceId, null);
AppCompat v23 已接受的答案已过时,请检查 sdk\sources\android-23\android\support\v4\view\LayoutInflaterCompatHC.java
的来源以了解 LayoutInflater.Factory
问题在 forceSetFactory2
中是如何解决的方法。
我的 LayoutInflater.Factory
(下面的代码示例)调用 onCreateView
并与 'com.android.support:support-v4:22.0.0' 一起正常工作的主要问题。但是当我移动到 'com.android.support:support-v4:22.1.0' 或更高时,不会调用 onCreateView
。我不明白为什么?
//From many fragments i Call hintManager.inflate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
layout = hintManager.inflate(inflater, R.layout.generate_code);
...
//here is HintManager method called from various fragments.
public View inflate(LayoutInflater inflater, int layoutResourceId) {
AttributeParser attributeParser = new AttributeParser();
LayoutInflater layoutInflater = attributeParser.getLayoutInflater(inflater);
final View v = layoutInflater.inflate(layoutResourceId, null);
//here AttributeParserFactory#onCreateView should be called, but it fails with 22.1+ support lib, but works with 22.0
attributeParser.setViewAttribute(v);
return v;
}
...
//example of factory, that works fine with 22.0 support lib
private Map<Integer, HashMap<Integer, String>> helpViewList;
private class AttributeParser {
private AttributeParserFactory mFactory;
private class AttributeParserFactory implements LayoutInflater.Factory{
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
String id = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "id");
if(id != null){
// String with the reference character "@", so we strip it to keep only the reference
id = id.replace("@", "");
TypedArray libraryStyledAttributeList = context.obtainStyledAttributes(attrs, R.styleable.Help);
HashMap<Integer, String> libraryViewAttribute = new HashMap<Integer, String>();
int i = 0;
for(int attribute : R.styleable.Help){
String attributeValue = null;
if (attribute == R.styleable.Help_arrow || attribute == R.styleable.Help_padding) {
attributeValue = String.valueOf(libraryStyledAttributeList.getInteger(i, 0));
}
else {
attributeValue = libraryStyledAttributeList.getString(i);
}
if(attributeValue != null)
libraryViewAttribute.put(attribute, attributeValue);
i++;
}
if(!libraryViewAttribute.isEmpty()) {
helpViewList.put(Integer.valueOf(id), libraryViewAttribute);
}
libraryStyledAttributeList.recycle();
}
return null;
}
}
public AttributeParser(){
mFactory = new AttributeParserFactory();
}
public void clear() {
helpViewList.clear();
}
public LayoutInflater getLayoutInflater(LayoutInflater inflater) {
LayoutInflater layoutInflater = inflater.cloneInContext(inflater.getContext());
layoutInflater.setFactory(mFactory);
return layoutInflater;
}
public void setFactory(LayoutInflater inflater){
inflater.cloneInContext(inflater.getContext()).setFactory(mFactory);
}
public void setViewAttribute(Activity activity) {
for(Map.Entry<Integer, HashMap<Integer, String>> attribute : helpViewList.entrySet())
if(activity.findViewById((Integer) attribute.getKey()) != null)
activity.findViewById((Integer) attribute.getKey()).setTag(attribute.getValue());
}
public void setViewAttribute(View view) {
for(Map.Entry<Integer, HashMap<Integer, String>> attribute : helpViewList.entrySet())
if(view.findViewById((Integer) attribute.getKey()) != null)
view.findViewById((Integer) attribute.getKey()).setTag(attribute.getValue());
}
public Map<Integer, HashMap<Integer, String>> getAttributeList() {
return helpViewList;
}
public void setAttributeList(Map<Integer, HashMap<Integer, String>> attributeList) {
helpViewList = attributeList;
}
}
编辑后的答案:
为了解决这个问题,我们应该使用 LayoutInflaterCompat.setFactory 来帮助 pre/post Lollipop 中的 LayoutInflater Factory。
这是例子
LayoutInflater layoutInflaterCopy = inflater.cloneInContext(inflater.getContext());
LayoutInflaterCompat.setFactory(layoutInflaterCopy, new LayoutInflaterFactory() {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
String id = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "id");
if(id != null){
// String with the reference character "@", so we strip it to keep only the reference
id = id.replace("@", "");
TypedArray libraryStyledAttributeList = context.obtainStyledAttributes(attrs, R.styleable.Help);
HashMap<Integer, String> libraryViewAttribute = new HashMap<Integer, String>();
int i = 0;
for(int attribute : R.styleable.Help){
String attributeValue = null;
if (attribute == R.styleable.Help_arrow || attribute == R.styleable.Help_padding) {
attributeValue = String.valueOf(libraryStyledAttributeList.getInteger(i, 0));
}
else {
attributeValue = libraryStyledAttributeList.getString(i);
}
if(attributeValue != null)
libraryViewAttribute.put(attribute, attributeValue);
i++;
}
if(!libraryViewAttribute.isEmpty()) {
helpViewList.put(Integer.valueOf(id), libraryViewAttribute);
}
libraryStyledAttributeList.recycle();
}
return null;
}
});
final View v = layoutInflaterCopy.inflate(layoutResourceId, null);
AppCompat v23 已接受的答案已过时,请检查 sdk\sources\android-23\android\support\v4\view\LayoutInflaterCompatHC.java
的来源以了解 LayoutInflater.Factory
问题在 forceSetFactory2
中是如何解决的方法。