LayoutInflater: use onCreateView() method from Activity if provided

This commit is contained in:
Julian Winkler
2025-01-11 17:47:49 +01:00
parent 6be60574aa
commit 1aba1c901d
2 changed files with 13 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Slog;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
@@ -34,7 +35,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Activity extends ContextThemeWrapper implements Window.Callback {
public class Activity extends ContextThemeWrapper implements Window.Callback, LayoutInflater.Factory2 {
private final static String TAG = "Activity";
public static final int RESULT_CANCELED = 0;
@@ -582,4 +583,8 @@ public class Activity extends ContextThemeWrapper implements Window.Callback {
throw new IllegalArgumentException("ID does not reference a View inside this View");
return view;
}
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return null;
}
}

View File

@@ -41,6 +41,7 @@ public class LayoutInflater {
}
public interface Factory2 {
public View onCreateView(View parent, String name, Context context, AttributeSet attrs);
}
private Factory2 mFactory2;
@@ -117,11 +118,14 @@ public class LayoutInflater {
Slog.v(TAG, tabs(indent) + "createViewFromTag called: parent: " + parent + ", name: " + name);
View view;
View view = null;
if (-1 == name.indexOf('.')) {
if (context instanceof Factory2) {
view = ((Factory2)context).onCreateView(parent, name, context, attrs);
}
if (view == null && -1 == name.indexOf('.')) {
view = onCreateView(parent, name, attrs);
} else {
} else if (view == null) {
view = createView(name, null, attrs);
}