ViewStub可以实现,布局的异步填充。当需要使用某一部分布局的时候,再去把它加载出来。
有点像苹果中的懒加载。什么时候需要,什么时候加载出来,节约内存空间,提高流畅度。
有两种方法去实现:
方法一(for example:vs_view1):
已在XML中写入布局ID(android:layout="@layout/viewstub_layout1"),直接inflate填充。
方法二(for example:vs_view2):
没有在XML中写入布局ID,需要设置布局ID后(setLayoutResource),inflate填充。
注意事项:
有点像苹果中的懒加载。什么时候需要,什么时候加载出来,节约内存空间,提高流畅度。
有两种方法去实现:
方法一(for example:vs_view1):
已在XML中写入布局ID(android:layout="@layout/viewstub_layout1"),直接inflate填充。
方法二(for example:vs_view2):
没有在XML中写入布局ID,需要设置布局ID后(setLayoutResource),inflate填充。
注意事项:
1.ViewStub.inflate();只能加载一次,加载之后会不可改变布局。
源代码Demo:demo下载地址点击下载
废话不说直接贴代码
布局如下:
MainActivity 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context="com.terry.viewstub.viewstubdemo.MainActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="viewstub" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/but_switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show_VS1" />
<Button
android:id="@+id/but_switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show_VS2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="horizontal">
<ViewStub
android:id="@+id/vs_view1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout="@layout/viewstub_layout1"/>
<ViewStub
android:id="@+id/vs_view2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
viewStub1布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@mipmap/img_vs"
android:id="@+id/imageView" />
</LinearLayout>
viewStub2布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
app:srcCompat="@mipmap/img_1"
android:id="@+id/imageView" />
<ImageView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
app:srcCompat="@mipmap/img_2"
android:id="@+id/imageView2" />
</LinearLayout>
MainActivity:
package com.terry.viewstub.viewstubdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ViewStub vs_view1,vs_view2;
Button but_switch_1,but_switch_2;
boolean isOpenvs1 = false, isOpenvs2 = false;
TextView tv_title;
View view1,view2;
/***
* Author: TerryYue
*
* Email: yue_zg@163.com
*
* ReadMe:
* ViewStub可以实现,布局的异步填充。当需要使用某一部分布局的时候,再去把它加载出来。
*
* 有点像苹果中的懒加载。什么时候需要,什么时候加载出来,节约内存空间,提高流畅度。
*
* 有两种方法去实现:
*
* 方法一(for example:vs_view1):
* 已在XML中写入布局ID(android:layout="@layout/viewstub_layout1"),直接inflate填充。
*
*
* 方法二(for example:vs_view2):
* 没有在XML中写入布局ID,需要设置布局ID后(setLayoutResource),inflate填充。
*
* 注意事项:
* 1.ViewStub.inflate();只能加载一次,加载之后会不可改变布局。
*/
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.but_switch_1:
//方法一:已在XML中写入布局ID(android:layout="@layout/viewstub_layout1"),直接inflate填充
if (view1==null) {
view1 = vs_view1.inflate();//每个ViewStub只能加载一次
}
if (isOpenvs1){
view1.setVisibility(View.GONE);
}else {
view1.setVisibility(View.VISIBLE);
}
isOpenvs1 = !isOpenvs1;
tv_title.setText("isOpenvs1 = "+isOpenvs1+" - isOpenvs2 = "+isOpenvs2);
break;
case R.id.but_switch_2:
//方法二:没有在XML中写入布局ID,需要设置布局ID后(setLayoutResource),inflate填充
if (view2==null) {
vs_view2.setLayoutResource(R.layout.viewstub_layout2);
view2 = vs_view2.inflate();//每个ViewStub只能加载一次
}
if (isOpenvs2){
view2.setVisibility(View.GONE);
}else {
view2.setVisibility(View.VISIBLE);
}
isOpenvs2 = !isOpenvs2;
tv_title.setText("isOpenvs1 = "+isOpenvs1+" - isOpenvs2 = "+isOpenvs2);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but_switch_1 = (Button)findViewById(R.id.but_switch_1);
but_switch_2 = (Button)findViewById(R.id.but_switch_2);
vs_view1 = (ViewStub)findViewById(R.id.vs_view1);
vs_view2 = (ViewStub)findViewById(R.id.vs_view2);
tv_title = (TextView)findViewById(R.id.tv_title);
but_switch_1.setOnClickListener(listener);
but_switch_2.setOnClickListener(listener);
tv_title.setText("isOpenvs1 = "+isOpenvs1+" - isOpenvs2 = "+isOpenvs2);
}
}