創建一個ObservableBoolean一個SplashStatus模型:
private static class SplashStatus {
public final ObservableBoolean isFinished = new ObservableBooelan();
}
這裏是你的飛濺佈局:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="status" type="com.example.SplashStatus"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Screen"
android:onFinish="@{status.isFinished}"/>
</LinearLayout>
</layout>
和有約束力的適配器方法:
@BindingAdapter("android:onFinish")
public static void finishSplash(View view, boolean isFinished) {
if(isFinished){
((Activity)(view.getContext())).startActivity(new Intent(view.getContext(), MainActivity.class))
((Activity)(view.getContext())).finish();
}
}
在SplashActivity.java
初始化數據綁定onCreate。每當你分配isFinished.set(true)
onFinished方法將啓動你的MainActivity並完成當前。