其他分享
首页 > 其他分享> > android-使用mvvp结构中的数据绑定导航到另一个活动

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

作者:互联网

我正在学习数据绑定库,对此我是全新的.我的问题是如何导航到有关数据绑定的其他活动.

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

解决方法:

好吧,这确实取决于用例,但理想情况下,每个操作都应通过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"
    />

标签:android-databinding,android-mvvm,android,mvvm
来源: https://codeday.me/bug/20191109/2011966.html