New UI-& lt; ViewStub & gt; tag delayed loading layout, ui-viewstub

Source: Internet
Author: User

New UI-<ViewStub> label delayed loading layout, ui-viewstub

New UI-<ViewStub> label delayed loading layout

-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!


The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.

After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.

For more details, help more people, O (∩ _ ∩) O thank you!

Piggy Android Development Exchange Group: Piggy Android Development Exchange Group No.: 421858269

New Android UI instance Daquan Directory: http://blog.csdn.net/coder_pig/article/details/42145907



This section introduces:

After learning the include layout reuse in the previous section, I believe everyone has realized the advantages of layout optimization,

However, when the include layout is complex controls, these controls are actually used.

When the process is not used much, the page loading speed is often reduced;You can also set

Visibility = "gone" attribute, but in the inflate layout, these controls are still inflate,

That is to say, the object will still be created, will be instantiated, set attributes, and so on, will still consume memory resources,

In this section, we will introduce a lightweight view: ViewStub

Load your layout as needed (delayed loading)

Now, start this section ~




Body of this section:


What is ViewStub?

ViewStub is a lightweight View that does not occupy any controls in the layout or participate in
The calculation and drawing of the layout (also called rendering) can be understood as a placeholder in the control tree,
There is nothing in place. When we need it, it will be rendered (loaded)
On the main interface, this behavior is also called delayed loading, or "inert include"


Where can I use it?

When a layout in our app does not need to show all the content, we can hide some View views,

You can use ViewStub for loading when you need to display it. It is a bit similarVisibility = "gone ",

But not along with the layoutFor example: (click the item in the listview to load the layout below)



How to use it? 1) Add the <ViewStub label> to the layout.

<ViewStub        android:id="@+id/stub_add"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:inflatedId="@+id/ly_add"        android:layout="@layout/ly_test" />

Three key attributes:

① Id: Set the reference id of ViewStub
② InflatedId: id of the loaded Layout
③ Layout: The layout to be loaded

Ps: The second property is optional. After loading ViewStub, we can directly use findViewById (R. id .~)

Find the object corresponding to the loaded layout,~ Is the id of inflatedId! Because ViewStub can only be used once !!!!



2) In the Activity that loads the layout file:

Method 1: After finding the ViewStub object through findViewById, call the inflate () method of the object:

ViewStub stub = (ViewStub) findViewById (R. id. stub_add );

Stub. inflate ();


Method 2: Change the visibility attribute of the ViewStub object

ViewStub stub = (ViewStub) findViewById (R. id. stub_add );

Stub. setVisibility (View. VISIBLE );

After writing the above, if we get the corresponding object for loading the layout, we need to use findViewById (id in R. id. inflatedId );

Find the corresponding layout object:

View ly_add = (View) findViewById (R. id. ly_add );



At the same time, there is also a way of writing:

Because after ViewStub loads the layout he references, he will remove it from the Control tree,

Therefore, if you want to perform related operations on the introduced layout, you should use a View.

Put this layout in place! You can also use the following statement:

ViewStub stub = (ViewStub) findViewById (R. id. stub_add );

View view = stub. inflate ();

Or:

ViewStub stub = (ViewStub) findViewById (R. id. stub_add );

View view = stub. setVisibility (View. VISIBLE );


Okay. You like what to use!



Note the following when using ViewStub:

1) ViewStub can only be inflate once, and then the ViewStub object will be left blankSo when writing code

Do not use variables with a long life cycle to reference the ViewStub control. Just follow the instructions above!

2) ViewStub loads only the layout File id, not a View.



Sample Code:

From the alias "inert include", we can know that ViewStub and include are actually similar.

However, the former is not rendered and rendered along with the layout, but when we set Inflate or Visibility to VISIBLE

Only rendering, and only one time, for one-time things, used more in ListView,

Of course, this is a simple example:

:



The error is over. Well, don't worry. paste the code first:

Actvitiy_main.xml:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. test. mainActivity "> <Button android: id =" @ + id/btnLoad "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "loading ViewStub"/> <Button android: id = "@ + id/btnHide" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "hide the loaded layout"/> <Button android: id = "@ + id/btnShow" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "display loaded layout"/> <ViewStub android: id = "@ + id/stub_add" android: layout_width = "match_parent" android: layout_height = "match_parent" android: layout_gravity = "center" android: inflatedId = "@ + id/ly_add" android: layout = "@ layout/ly_test"/> </LinearLayout>

Introduced layout: ly_test.xml:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. test. mainActivity "android: gravity =" center "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: background =" @ drawable/back "android: text = "layout introduced by ViewStub for testing" android: textColor = "# FFFFFF" android: gravity = "center_vertical" android: textSize = "18sp"/> </LinearLayout>

Code in MainActivity. java:

package com.jay.example.test;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewStub;import android.widget.Button;public class MainActivity extends Activity {private Button btnLoad;private Button btnHide;private Button btnShow;private MyClick myClick;private ViewStub viewStub;private View view;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();setUpViews();}private void findViews() {btnLoad = (Button) findViewById(R.id.btnLoad);btnHide = (Button) findViewById(R.id.btnHide);btnShow = (Button) findViewById(R.id.btnShow);}private void setUpViews() {myClick = new MyClick();btnLoad.setOnClickListener(myClick);btnHide.setOnClickListener(myClick);btnShow.setOnClickListener(myClick);}private class MyClick implements OnClickListener {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnLoad:viewStub = (ViewStub) findViewById(R.id.stub_add);view = viewStub.inflate();break;case R.id.btnHide:view.setVisibility(View.INVISIBLE);break;case R.id.btnShow:view.setVisibility(View.VISIBLE);break;}}}}


Code Analysis:

The code is very simple. The question is why an error is reported when ViewStub is loaded twice.

NullPointerException. This is because ViewStub can only be inflate once.

Therefore, the subsequent implementation of hiding and displaying is to control the layout object introduced !!!




The last two sentences are as follows:

The use of ViewStub is very simple, but it is also very important. You may not think of it now, but there are more controls on the interface,

That's all you know! Come on ~






References in this section:

Http://www.cnblogs.com/plokmju/p/android_viewstub.html

Http://blog.csdn.net/mayingcai1987/article/details/6238609

Http://blog.csdn.net/hitlion2008/article/details/6737537

Https://developer.android.com/reference/android/view/ViewStub.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.