Android layout optimization (I) view stub details

Posted by Spinicrus on Sun, 29 Mar 2020 20:47:04 +0200

1.ViewStub inheritance relationship

public final class ViewStub 
extends View 
java.lang.Objectandroid.view.Viewandroid.view.ViewStub

The definition of ViewStub on the official website: a ViewStub is an invisible, zero sized view that can be used to lazily inflate layout resources at runtime.
This means that view stub is an invisible zero size view used to delay loading layout resources at run time.

2. Lazy loading principle of viewstub:

<ViewStub 
       android:id="@+id/stub"
       android:inflatedId="@+id/subTree"
       android:layout="@layout/mySubTree"
       android:layout_width="120dip"
       android:layout_height="40dip" />

When the member method setVisibility(int) or inflate() of ViewStub is called, the ViewStub itself is replaced by the layout corresponding to mySubTree, and the layout parameters in ViewStub also replace the layout parameters in mySubTree root layout. For example, inflated id will replace the id of mySubTree root layout, layout with, layout with height will replace the width and height set in mySubTree layout, i.e. 120dip above, 40DIP will be set to the width and height of the root layout of mySubTree, which is invalid.
3. Use of viewstub

Method 1:
ViewStub stub = findViewById(R.id.stub);
View inflated = stub.inflate();

Mode two:
ViewStub stub = findViewById(R.id.stub);
stub.setVisiblity(View.Visible);

Example:
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="showStub" />

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/layout_include" />
 </LinearLayout>

layout_include.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ViewStub" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello" />

</LinearLayout>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    private final String TAG = getClass().getSimpleName();
    private LinearLayout ll;
    private Button btn1, btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    private void initData() {
        findViewById(R.id.btn_stub).setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        //One way
//        ViewStub viewStub = findViewById(R.id.view_stub);
//        if (ll == null) {
//            //The inflate() method returns the layout to be loaded
//            ll = (LinearLayout) viewStub.inflate();
//            btn1 = findViewById(R.id.btn1);
//            btn2 = findViewById(R.id.btn2);
//        } else {
//            //Layout loaded
//        }
//        btn1.setText("hello");
//        btn2.setText("world");
//    Mode two
        ViewStub viewStub = findViewById(R.id.view_stub);
        if (ll == null) {
            viewStub.setVisibility(View.VISIBLE);
            ll = findViewById(R.id.linear1);
        } else {
            Toast.makeText(this, "Layout loaded", Toast.LENGTH_SHORT).show();
        }


    }
}

The above two methods are respectively the practice of inflate() and setVisibility() methods.

Topics: Android xml Java encoding