使用mvvp结构中的数据绑定导航到另一个活动

时间:2018-03-31 05:38:01

标签: android mvvm android-databinding android-mvvm

我正在学习数据绑定库,我对此完全陌生。我的问题是如何导航到另一个关于数据绑定的活动。

我可以像在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代码?如果是的话,那该怎么办?

1 个答案:

答案 0 :(得分:3)

嗯,这实际上取决于用例,但理想情况下,每个操作都应该通过ViewModel。

如果您需要在导航之前做一些事情,比如存储数据,那么您的点击操作应该来自ViewModel。如果你只需要重定向,你可以用MVC方式做。

我通常以这种方式这样做:

SplashViewModel.java

public static final int ACTION_NAVIGATE_TO_GAME = 1000;

public void navigateToGame() {
    // You can do some work here before notifying the view about redirection
    setChanged();
    notifyObserver(ACTION_NAVIGATE_TO_GAME);
}

SplashActivity.java

@Override
public void update(Observable observable, Object data) {
    if (data instanceof Integer) {
        int value = (int) data;
        switch(value) {
           case SplashViewModel.ACTION_NAVIGATE_TO_GAME:
                startActivity(new Intent(SplashActivity.this,GameActivity.class));
                break;
        }
    }
}

并在布局文件中

<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"
            android:onClick="@{() -> splashViewModel.navigateToGame()}"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
    />