我正在尝试检查用户是否已登录。如果是,则显示特定的视图组,否则显示不同的视图组。要检查用户是否已登录,我正在从共享首选项中获取用户(登录时用户保存在共享首选项中)。让我展示我的代码。
飞溅视图模型
public class SplashViewModel extends ViewModel {
public final String TAG = "SplashViewModel";
private final String GREETING = "Hi ";
public ObservableBoolean isLoggedIn = new ObservableBoolean();
public ObservableField<String> userName = new ObservableField<>("");
private Preference<User> preference;
SplashViewModel(Preference preference) {
this.preference = preference;
isLoggedIn.set(false);
}
public void getCurrentUser() {
preference.get(Constants.CURRENT_USER, User.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(user -> {
isLoggedIn.set(true);
userName.set(GREETING + user.name);
}, throwable -> isLoggedIn.set(false));
}
}
活动飞溅
<?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">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/loggedin_layout"
android:visibility="@{splashViewModel.isLoggedIn ? View.VISIBLE : View.GONE,default=gone}">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@{splashViewModel.userName}"
android:textColor="@color/white"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.40"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="Welcome Back"
android:textColor="@color/white"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.50"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Start a New Game >>"
android:textColor="@color/white"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.70"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/loggedout_layout"
android:visibility="@{splashViewModel.isLoggedIn ? View.GONE : View.VISIBLE}"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="Login"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/login_button"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="Sign Up"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/sign_up_button"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="View Scores"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/view_scores_button"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintVertical_bias="0.66"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<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>
</android.support.constraint.ConstraintLayout>
</layout>
飞溅活动
public class SplashActivity extends AppCompatActivity {
SplashViewModel splashViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataBinding();
setButtonClickListeners();
}
@Override
protected void onStart() {
super.onStart();
splashViewModel.getCurrentUser();
}
private void initDataBinding() {
ActivitySplashBinding activitySplashBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
SplashViewModelFactory splashViewModelFactory = Injection.provideSplashViewModelFactory(this);
splashViewModel = ViewModelProviders.of(this, splashViewModelFactory).get(SplashViewModel.class);
activitySplashBinding.setSplashViewModel(splashViewModel);
// splashViewModel.getCurrentUser();
}
private void setButtonClickListeners() {
findViewById(R.id.guest_button).setOnClickListener(v -> {
startActivity(new Intent(SplashActivity.this, GameActivity.class));
finish();
});
findViewById(R.id.sign_up_button).setOnClickListener(v -> startActivity(
new Intent(SplashActivity.this, RegisterActivity.class)));
findViewById(R.id.login_button).setOnClickListener(v -> startActivity(
new Intent(SplashActivity.this, LoginActivity.class)));
findViewById(R.id.view_scores_button).setOnClickListener(v -> startActivity(
new Intent(SplashActivity.this, ScoresActivity.class)));
}
}
但是当我尝试运行时,它给了我以下错误。
错误:找不到符号类 ActivitySplashBinding
有人可以帮我吗?