See on-line View.findviewbyid () and Activity.findviewbyid () execution efficiency is not the same
Use Activity.findviewbyid () such as:
TextView tv_inner_1 = (TextView)this. Findviewbyid (r.id.tv_inner_1); = (TextView)this. Findviewbyid (r.id.tv_inner_2);
Use View.findviewbyid () such as:
This . Findviewbyid (r.id.layout_outer); = (TextView) Layout_outer.findviewbyid (r.id.tv_inner_1); = (TextView) Layout_outer.findviewbyid (r.id.tv_inner_2);
They're all for the same XML
<LinearLayout> <LinearLayoutID= "@+id/layout_outer"> <TextViewID= "@+id/tv_inner_1"/> <TextViewID= "@+id/tv_inner_2"/> </LinearLayout> </LinearLayout>
Since learning about Android's Hello World begins
We knew that a function such as Findviewbyid (), he has become a household name, swindling, kill anyone must have a function (well, this sentence is nonsense)
But has been used and did not carefully study it, until the writing process found a strange bug caused by this function, so decided to study the next function ~
The Findviewbyid () function that we call actually has two kinds (at present I only see two kinds, unsure whether there is no other), one is the activity class in the Findviewbyid () function
The other is the Findviewbyid () function defined in the view class
In general, the (**view) Findviewbyid (r.id.**) that we use in the OnCreate () method is both the Findviewbyid () function in the invoked activity
The Findviewbyid () in the view class is called in ***view.findviewbyid ( ), which is written in other cases.
Take a look at the implementation method and introduction in the source code separately
Findviewbyid () defined in the Activity class
/*** Finds A view that is identified by the id attribute from the XML that is processed in {@link#onCreate}. * * @returnThe view if found or null otherwise.*/ PublicView Findviewbyid (intID) {returnGetWindow (). Findviewbyid (ID);} /*** Retrieve the current {@linkAndroid.view.Window} for the activity. * This can is used to directly access parts of the Windows API that * is not available through Activity/screen. * * @returnwindow The current window, or null if the activity was not * visual.*/ PublicWindow GetWindow () {returnMwindow;}
Here you can see that this function is looking for an object with the specified ID defined in the XML
Findviewbyid () in the View class
/*** Look for a child view with the given ID. If This view has the given * ID, return this view. * * @paramID The ID to search for. *@returnThe view that have the given ID in the hierarchy or null*/ Public FinalView Findviewbyid (intID) {if(ID < 0) { return NULL; } returnfindviewtraversal (ID); /*** {@hide} *@paramID of the The view to be found *@returnThe view of the specified ID, null if cannot be found*/ protectedView Findviewtraversal (intID) {if(id = =MID) {return This; } return NULL; }
From this we can see that we are looking for the object of the specified ID from a view's child view, so even if the ID number of the view in several layout XML files is the same, as long as they do not have the same parent node, or have the same Father node, But it is no problem to call Findviewbyid by ID to find them without the parent node and above nodes. (This quote from here http://www.2cto.com/kf/201204/127404.html)
Frequently asked questions about using this function:
1. Since Findviewbyid () in the activity is looking for something from the R.java, then we have to eliminate the control of the same name
Today's own bugs are the result of this.
When it comes to naming controls, there's a little discovery today.
Take a closer look at the two code codes below
<? xmlversion= "1.0"encoding= "Utf-8"?> <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/linearlayout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > </linearlayout>
<? xmlversion= "1.0"encoding= "Utf-8"?> <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > </linearlayout>
There is no ID in the layout of this parameter, a part of the ID, although the code is different but shown in the outline is
So in the first case r.id can find linearlayout this control, the second is not the HA, these are also the details to be noted later
2. Findviewbyid in Call view () Be sure to think of who the parent view is! The **view in **view.findviewbyid () is to find the right, and if you don't find the parent view, the return is basically null.
View.findviewbyid () and Activity.findviewbyid () differences