Android MVVM简单使用

MVVM需要用到DataBinding知识,参考Android DataBinding简单使用

1、生成DataBinding布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="viewModel"
            type="com.app.mvvmdemo.MVVMViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="@{viewModel.result}" />

        <EditText
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="@={viewModel.userInput}"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:onClick="@{viewModel.getData}"
            android:text="替换" />
    </LinearLayout>
</layout>

2、实现MVVMViewModel

public class MVVMViewModel extends BaseObservable {
    private MVVMModel mvvmModel;
    //EditText数据
    private String userInput;
    //网络返回数据
    private String result;

    public MVVMViewModel(Application application){
        mvvmModel = new MVVMModel();
    }

    public void getData(View view){
        mvvmModel.getHttpData(userInput, new MCallback() {
            @Override
            public void onSuccess(String text) {
                setResult(text);
            }

            @Override
            public void onFailed() {
                setResult("数据获取失败");
            }
        });
    }

    @Bindable
    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
        notifyPropertyChanged(BR.result);
    }

    public String getUserInput() {
        return userInput;
    }

    public void setUserInput(String userInput) {
        this.userInput = userInput;
    }
}
public class MVVMModel {

    //模拟网络请求获取数据
    public void getHttpData(String user, MCallback callback){
        Random random = new Random();
        boolean isSuccess = random.nextBoolean();
        if (isSuccess){
            callback.onSuccess("数据获取成功:" + user);
        } else {
            callback.onFailed();
        }
    }
}

3、Activity代码

public class MVVMActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMvvmBinding binding =
                DataBindingUtil.setContentView(this, R.layout.activity_mvvm);

        //绑定ViewModel
        MVVMViewModel mvvmViewModel = new MVVMViewModel(getApplication());
        binding.setViewModel(mvvmViewModel);
    }
}

上一篇:(转)Jmeter内存溢出处理方式记录


下一篇:面试-MVC和MVVM区别-Vue为什么没有完全遵守MVVM?