Does findViewById() perform poorly on Android?
What algorithms are used internally to find Id?
And I wonder why the performance is poorㅠ<
Finally, what function can replace findViewById()?
android findviewbyid performance
Technically, the activity calls findViewById
to the main decorator view. After that, findViewById
is repeatedly called depending on the hierarchy of the view.
If View
, make sure that you have the same ID as yourself.
In the case of ViewGroup
, compare whether you and your id are the same or not, and call findViewById
one by one for your children if they are not the same.
Individual views identify themselves, and those responsible for the layout identify themselves and their children. This process is the same as depth-first navigation in a tree.
The time findViewById
is called is not a big concern unless the view is too deep or has many children.
Also, for commonly used findViewById
, find the view in onCreate
and register it as an attribute of an activity or fragment class.
mTextView = findViewById(R.id.testView);
...
mTextView.setText("Blah Blaah");
It's more effective if you keep the view that you've found in this way, so you don't have to ask again. It's quite a hassle to manually do the process of storing the view you's been found. For such operations, you can automate them through annotation processing. A typical library is Butterknife.
You can also use data bindings to synchronize data from a view without finding it. See Data Binding if you want to know how.
© 2024 OneMinuteCode. All rights reserved.