Viewstub is a good tab/control in Android layout optimization and inherits from view directly. Although Android developers have largely heard of it, they may not really use it much.
Viewstub can be understood as a very lightweight view, with its own attributes and specific methods, like other controls. When Viewstub is used in a layout file, when the program inflate the layout file, the viewstub itself is parsed and occupies the memory control, but the main difference is in the following points compared to other controls:
1. When the layout file is inflate, the Viewstub control also occupies memory, but compared to other controls, the viewstub memory is small;
2. When the layout file is inflate, viewstub is primarily a "placeholder" character, placed in the view tree, and viewstub itself is not visible. Viewstub has a layout attribute that points to a layout file that the viewstub itself may be replaced, and, at a certain time, through Viewstub.inflate () to complete the process;
3.ViewStub itself is not visible to the viewstub setvisibility (..) Unlike other controls, Viewstub's setvisibility View.visible or invisible If it is used for the first time, it automatically inflate the layout file it points to and replaces the viewstub itself, which is equivalent to setting visibility to the layout file it points to.
Here's what you need to be aware of:
1.ViewStub is often referred to as "deferred loading" because, in most cases, the program does not need to display the layout file that viewstub points to, and only under certain few conditions, the layout file that Viewstub points to is inflate. And this layout file directly replaces the current viewstub, specifically through Viewstub.infalte () or viewstub.setvisibility (view.visible);
2. It is very important to correctly grasp the application scenario of Viewstub, as described in the requirements scenario in 1, viewstub can be optimized for layout.
3. The inflate operation for Viewstub can only be done once, because inflate is the time to parse the layout file it points to inflate and replace the current viewstub itself (thus reflecting the viewstub "placeholder" nature), once replaced, There is no viewstub control in the original layout file, so if you infalte the viewstub multiple times, you receive an error message: Viewstub must have a non-null viewgroup.
The layout file referred to in 4.3 resolves inflate and replaces the current viewstub itself, and is not a complete replacement (unlike the include tag), when the layout file's layout is replaced by the Viewstub Params is based on viewstub, and other layout attributes are based on the layout file itself.
Here is a simple demand scenario: When the ListView display list data, there may be a service-side data is not the case, the display of a emptyview, prompts the user temporarily no data. At this point, considering the actual application of the Emptyview display opportunities are relatively small, so you can use the Viewstub positioning in the layout file, and then do not have the data to Viewstub.infalte ().
The relevant section of the code is as follows:
public void Showemptyview () {
listview.setvisibility (view.gone);
if (Nodataview = = null) {
viewstub nodataviewstub = (viewstub) View.findviewbyid (r.id.no_data_viewstub);
Nodataview = Nodataviewstub.inflate ();
} else {
nodataview.setvisibility (view.visible);
}
}
public void Showlistview () {
listview.setvisibility (view.visible);
if (Nodataview!= null) {
nodataview.setvisibility (view.gone);
}
}
Special attention should be paid to whether the viewstub has been inflate judgment.
In ListView item, you may sometimes experience scenarios where the layout of item on a different list page is different, but not much compared to the entire item layout, where two of the most common treatments are as follows :
1. Write out the different parts, put them in an item file, and then logically handle the display of different parts separately (view.visible and View.gone);
2. The whole part of the two different item separately, completely written two item file, and then combined with ListView display different layout to do logical processing (through Getitemtype (), etc.).
Both of these methods can be used in fact, the first way logic clear, very flexible, but to some extent, increased memory and resource consumption. The second way is that the layout files have duplicates (although the same part can be included, but logically there are duplicates), including the actual duplication of the logically processed code. This is generally recommended for a larger variety of item layouts.
Sometimes the combination of requirements, can be in the first way, based on the combination of viewstub "placeholder" can be better to complete such requirements. It is also quite a compromise of two ways, but it takes into account both memory and resource consumption and different layout logic controls.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.