gpt4 book ai didi

android - ViewModel类应该包含Android元素吗?

转载 作者:行者123 更新时间:2023-12-03 11:02:31 24 4
gpt4 key购买 nike

从MVP转到MVVM,并尝试从Web教程中学习。

一些教程指出,ViewModel类不应对Activity或View(android.view.View)类有任何引用。

但是在我看过的一些教程中,ViewModel类和Activity中使用了Views,以使用ViewModel启动其他Activity。
For example:

import android.arch.lifecycle.ViewModel;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.journaldev.androidmvvmbasics.interfaces.LoginResultCallback;
import com.journaldev.androidmvvmbasics.model.User;

public class LoginViewModel extends ViewModel {
private User user;
private LoginResultCallback mDataListener;

LoginViewModel(@NonNull final LoginResultCallback loginDataListener) {
mDataListener = loginDataListener;
user = new User("", "");
}


public TextWatcher getEmailTextWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void afterTextChanged(Editable editable) {
user.setEmail(editable.toString());
}
};
}

public TextWatcher getPasswordTextWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
user.setPassword(editable.toString());
}
};
}


public void onLoginClicked(@NonNull final View view) {
checkDataValidity();
}

private void checkDataValidity() {
if (user.isInputDataValid())
mDataListener.onSuccess("Login was successful");
else {
mDataListener.onError("Email or Password not valid");
}
}
}

View.OnClickListenerAnother one
public class PostViewModel extends BaseObservable {

private Context context;
private Post post;
private Boolean isUserPosts;

public PostViewModel(Context context, Post post, boolean isUserPosts) {
this.context = context;
this.post = post;
this.isUserPosts = isUserPosts;
}

public String getPostScore() {
return String.valueOf(post.score) + context.getString(R.string.story_points);
}

public String getPostTitle() {
return post.title;
}

public Spannable getPostAuthor() {
String author = context.getString(R.string.text_post_author, post.by);
SpannableString content = new SpannableString(author);
int index = author.indexOf(post.by);
if (!isUserPosts) content.setSpan(new UnderlineSpan(), index, post.by.length() + index, 0);
return content;
}

public int getCommentsVisibility() {
return post.postType == Post.PostType.STORY && post.kids == null ? View.GONE : View.VISIBLE;
}

public View.OnClickListener onClickPost() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
Post.PostType postType = post.postType;
if (postType == Post.PostType.JOB || postType == Post.PostType.STORY) {
launchStoryActivity();
} else if (postType == Post.PostType.ASK) {
launchCommentsActivity();
}
}
};
}

public View.OnClickListener onClickAuthor() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(UserActivity.getStartIntent(context, post.by));
}
};
}

public View.OnClickListener onClickComments() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
launchCommentsActivity();
}
};
}

private void launchStoryActivity() {
context.startActivity(ViewStoryActivity.getStartIntent(context, post));
}

private void launchCommentsActivity() {
context.startActivity(CommentsActivity.getStartIntent(context, post));
}
}

带有 Activity 引用的 Another one
public class UserProfileViewModel {

/* ------------------------------ Constructor */

private Activity activity;

/* ------------------------------ Constructor */

UserProfileViewModel(@NonNull Activity activity) {
this.activity = activity;
}

/* ------------------------------ Main method */

/**
* On profile image clicked
*
* @param userName name of user
*/
public void onProfileImageClicked(@NonNull String userName) {

Bundle bundle = new Bundle();
bundle.putString("USERNAME", userName);
Intent intent = new Intent(activity, UserDetailActivity.class);
intent.putExtras(bundle);
activity.startActivity(intent);
}

/**
* @param editable editable
* @param userProfileModel the model of user profile
*/
public void userNameTextChange(@NonNull Editable editable,
@NonNull UserProfileModel userProfileModel) {

userProfileModel.setUserName(editable.toString());
Log.e("ViewModel", userProfileModel.getUserName());
}
}
  • 是否可以让ViewModel类包含Android和View类,
    这样对单元测试不利吗?
  • 自定义 View 模型类应该扩展哪个类? ViewModel
    BaseObservable/Observable
  • 是否有任何教程链接显示MVVM的简单用法以及
    只专注于没有任何Dagger2,LiveData或RxJava的体系结构
    扩展名?我现在只在寻找MVVM教程。
  • 最佳答案

    documentation:

    Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.



    这是因为 ViewModel在配置更改后仍然存在。假设您有一项 Activity ,并且旋转设备。该 Activity 被杀死,并创建了一个新实例。如果将 View 放在 View 模型中,则该 Activity 将不会被垃圾回收,因为 View 包含对先前 Activity 的引用。同样, View 本身将被重新创建,但是您将旧 View 保留在viewmodel中。基本上,不要在 View 模型中放置任何 View ,上下文, Activity 。

    这是来自Google的示例: https://github.com/googlesamples/android-sunflower/

    关于android - ViewModel类应该包含Android元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946050/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com