MVVM -- 实现

一、Model层

接口:

public interface IDataManager {
    String getMyLocation();
}

实现:

public class DataManager implements IDataManager {
    @Override
    public String getMyLocation() {
        return "北京";
    }
}

二、数据类

//Location
public class Location extends BaseObservable {
    @Bindable
    private String location;

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
        notifyPropertyChanged(com.tomorrow.mvvmtest.BR.location);
    }
}

三、View层

布局文件:

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable
        name="location"
        type="com.tomorrow.mvvmtest.viewmodel.Location" />
    <variable
        name="locationViewModel"
        type="com.tomorrow.mvvmtest.viewmodel.LocationViewModel"/>
</data>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/main_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="@{location.location}" /> //数据绑定
    <Button
        android:id="@+id/main_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/main_tv"
        android:layout_marginTop="10dp"
        android:textAllCaps="false"
        android:textSize="20sp"
        android:text="Get My Location"
        android:onClick="@{locationViewModel.getMyLocation}"/> //事件绑定
</RelativeLayout>
</layout>

Activity:

//MainActivity
public class MainActivity extends Activity {
    private ILocationViewModel mLocationViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mLocationViewModel = new LocationViewModel(binding);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationViewModel.onDestroy();
        mLocationViewModel = null;
    }
}

三、ViewModel层

ViewModel接口:

//ILocationViewModel
public interface ILocationViewModel {
    void onDestroy();
}

ViewModel实现:

//LocationViewModel
public class LocationViewModel implements ILocationViewModel{
    private ActivityMainBinding mBinding;
    private Location mLocation;
    private IDataManager mDataManager;

    public LocationViewModel(ActivityMainBinding binding) {
        mBinding = binding;
        mDataManager = new DataManager();
        initBinding();
    }

    private void initBinding() {
        //数据绑定
        mLocation = new Location();
        mLocation.setLocation("My Location");
        mBinding.setLocation(mLocation);
        //事件绑定
        mBinding.setLocationViewModel(this);
    }

    //事件绑定响应方法
    public void getMyLocation(View view) {
        String location = mDataManager.getMyLocation();
        //更新数据,同时会自动更新界面
        mLocation.setLocation(location);
    }

    @Override
    public void onDestroy() {
        mBinding = null;
        mLocation = null;
        mDataManager = null;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,827评论 25 709
  • 梦 六年级三班 王帅 梦就像一棵开满花的树,想要闻闻它。咦,变成果子了? 梦就像一条自由自在的鱼,想要摸摸它,咦,...
    燕归来yy阅读 415评论 0 2