使用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);
 
  • 效果

Android优化——UI优化(三)使用ViewStub延迟加载-LMLPHP

05-03 22:02