3

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

我可以像我们在 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 代码放在视图模型中吗?如果是,那么该怎么做?

4

1 回答 1

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"
    />
于 2018-03-31T06:06:16.790 回答