使用ViewStub
延迟加载
1.ViewStub
延迟加载
- xml
<ViewStub
android:id="@+id/stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="@+id/subTree"
android:layout="@layout/activity_viewstub_item"/>
- java
//获取到viewstub
final ViewStub stub = (ViewStub) findViewById(R.id.stub);
//测试用inflate()填充布局
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
View view = stub.inflate();
}
};
handler.sendEmptyMessageDelayed(1, 7000);
- 效果