findViewById(): only search among child views

also set default content view in Window
This commit is contained in:
Julian Winkler
2024-02-11 12:33:58 +01:00
parent d2f28a5b21
commit d253bfd24f
4 changed files with 24 additions and 8 deletions

View File

@@ -239,7 +239,9 @@ public class Activity extends ContextWrapper implements Window.Callback {
public <T extends android.view.View> T findViewById(int id) { public <T extends android.view.View> T findViewById(int id) {
System.out.println("- findViewById - asked for view with id: " + id); System.out.println("- findViewById - asked for view with id: " + id);
View view = View.view_by_id.get(id); View view = null;
if (window.contentView != null)
view = window.contentView.findViewById(id);
System.out.println("- findViewById - found this: " + view); System.out.println("- findViewById - found this: " + view);
return (T)view; return (T)view;

View File

@@ -808,8 +808,6 @@ public class View extends Object {
public long widget; // pointer public long widget; // pointer
public static HashMap<Integer, View> view_by_id = new HashMap<Integer, View>();
private int oldWidthMeasureSpec = -1; private int oldWidthMeasureSpec = -1;
private int oldHeightMeasureSpec = -1; private int oldHeightMeasureSpec = -1;
private boolean layoutRequested = true; private boolean layoutRequested = true;
@@ -836,7 +834,6 @@ public class View extends Object {
int id = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "id", 0); int id = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "id", 0);
if (id != 0) { if (id != 0) {
this.id = id; this.id = id;
view_by_id.put(id, this);
} }
@@ -860,8 +857,10 @@ public class View extends Object {
} }
public View findViewById(int id) { public View findViewById(int id) {
/* TODO: this should probably only search among children, but the id is surely unique anyway? :P */ if (this.id == id)
return view_by_id.get(id); return this;
else
return null;
} }
public void onDraw(Canvas canvas) {} public void onDraw(Canvas canvas) {}
@@ -946,7 +945,6 @@ public class View extends Object {
public void setId(int id) { public void setId(int id) {
this.id = id; this.id = id;
view_by_id.put(id, this);
} }
public void setOnKeyListener(OnKeyListener l) {} public void setOnKeyListener(OnKeyListener l) {}

View File

@@ -304,6 +304,17 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
public void setClipToPadding(boolean clipToPadding) {} public void setClipToPadding(boolean clipToPadding) {}
public View findViewById(int id) {
if (this.id == id)
return this;
for (View child: children) {
View result = child.findViewById(id);
if (result != null)
return result;
}
return null;
}
public static class LayoutParams { public static class LayoutParams {
public static final int FILL_PARENT = -1; public static final int FILL_PARENT = -1;
public static final int MATCH_PARENT = -1; public static final int MATCH_PARENT = -1;

View File

@@ -28,6 +28,8 @@ public class Window {
public Window(Window.Callback callback) { public Window(Window.Callback callback) {
this.callback = callback; this.callback = callback;
contentView = new ViewGroup(Context.this_application);
contentView.setId(android.R.id.content);
} }
public void addFlags(int flags) {} public void addFlags(int flags) {}
@@ -66,7 +68,10 @@ public class Window {
} }
public View findViewById(int id) { public View findViewById(int id) {
return View.view_by_id.get(id); if (contentView != null)
return contentView.findViewById(id);
else
return null;
} }
public View peekDecorView() { public View peekDecorView() {