Frage im Vorstellungsgespräch bei Booking.com

Provide an implementation for View findViewById(ViewGroup root, int id) using ViewGroup and View

Antworten zu Vorstellungsgespräch

Anonym

5. Nov. 2017

ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView(); TextView textView = (TextView) findViewById(root, R.id.text); textView.setText("Found " + textView.getText()); private View findViewById(ViewGroup root, int id) { if (root.getId() == id) return root; for (int i = 0; i < root.getChildCount(); i++) { View child = root.getChildAt(i); if (child.getId() == id) return child; if (child instanceof ViewGroup) { View view = findViewById((ViewGroup) child, id); if (view != null) return view; } } return null; }

Anonym

14. Sept. 2019

public View findViewById(ViewGroup root, int id) { if (root.getId() == id) { return root; } for (int viewIndex = 0; viewIndex < root.getChildCount(); viewIndex++) { View childView = root.getChildAt(viewIndex); if (childView.getId() == id) { return childView; } if (childView instanceof ViewGroup) { View grandChildView = findViewById((ViewGroup) childView, id); if (grandChildView != null) { return grandChildView; } } } return null; }