我正在学习数据绑定库,对此我完全陌生。我的问题是如何导航到数据绑定的另一个活动。
我可以像我们在 MVC 中那样做吗?或者我必须把代码放在 ViewModel 中。请帮我。
这是我的代码。
活动:
public class SplashActivity extends AppCompatActivity implements Observer {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataBinding();
initViews();
}
private void initViews() {
findViewById(R.id.guest_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SplashActivity.this,GameActivity.class));
}
});
}
private void initDataBinding() {
ActivitySplashBinding activitySplashBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
SplashViewModel splashViewModel = new SplashViewModel();
activitySplashBinding.setSplashViewModel(splashViewModel);
splashViewModel.addObserver(this);
}
@Override
public void update(Observable observable, Object data) {
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.sevenbits.android.mvvmsample.view.SplashActivity">
<data>
<variable name="splashViewModel"
type="com.sevenbits.android.mvvmsample.viewmodel.SplashViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/parent1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:text="Login"
android:textColor="@color/white"
android:textSize="24sp"
android:id="@+id/login_button"
app:layout_constraintBottom_toTopOf="@+id/parent1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/parent1"
app:layout_constraintVertical_bias="0.42"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:text="Sign Up"
android:textColor="@color/white"
android:textSize="24sp"
android:id="@+id/sign_up_button"
app:layout_constraintBottom_toTopOf="@+id/parent1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/parent1"
app:layout_constraintVertical_bias="0.58"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:textColor="@color/white"
android:textAllCaps="false"
android:textSize="18sp"
android:id="@+id/guest_button"
android:layout_marginBottom="20dp"
android:text="Play As a Guest User"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</layout>
我需要将 onclick 代码放在视图模型中吗?如果是,那么该怎么做?