一 VM使用
- 创建类A继承ViewModel
- 在A中写入基础属性
- 在Activity中创建A
MainVM mainVM=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MainVM.class);
二 LiveData+ViewModel
- 创建vm 继承 ViewModel
- 在vm中添加livedata属性
private MutableLiveData<Integer> count;
- 添加count的get方法
public MutableLiveData<Integer> getCount() {
if (count==null){
count=new MutableLiveData<>();
count.setValue(0);
}
return count;
}
- 在Activity中创建VM
mainVM = new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MainVM.class);
- 添加监听
mainVM.getCount().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
tv.setText(String.valueOf(integer));
}
});
三 databinding
1 简单使用
- 添加依赖
//启用DataBinding
buildFeatures {
dataBinding true
}
- 布局文件修改为支持数据绑定 ,第一行option+enter
- 编写Model
public class User {
public String name;
public int lever;
public User(String account, int pwd) {
this.name = account;
this.lever = pwd;
}
}
- 将Model放入布局中文件中
<data>
<variable
name="user"
type="com.example.myapplication.User" />
</data>
- 在布局文件中使用
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="@{user.name}"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
2 import 标签 和 事件绑定
A. import标签
- 创建类StarUtils
public class StarUtils {
public static String start(int start){
switch (start){
case 1:
return "1 星";
case 2:
return "2 星";
case 3:
return "3 星";
case 4:
return "4 星";
case 5:
return "5 星";
}
return "";
}
}
- 布局文件中引入
<data>
<import type="com.example.myapplication.StarUtils"/>
</data>
- 使用@{类名.方法名(方法参数)}
<TextView
android:id="@+id/tv_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{StarUtils.start(user.lever)}"
B 事件绑定
- 创建类EventHandleListen
public class EventHandleListen {
private Context context;
public EventHandleListen(Context context) {
this.context = context;
}
public void buttonClick(View view){
Toast.makeText(context,"wocao",Toast.LENGTH_SHORT).show();
}
}
- 在布局文件中引入
<data>
<variable
name="eventHandle"
type="com.example.myapplication.EventHandleListen" />
</data>
- 在布局文件中使用@{类的替代名.方法}
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:text="Button"
android:onClick="@{eventHandle.buttonClick}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
3 二级页面传递
- 创建二级页面sub.xml
- 在一级页面中找到Containers->将sub.xml 添加进来
<include
layout="@layout/sub"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2"
app:user="@{user}" />
- 在二级页面中添加变量user,一级变量中也要存在
<data>
<variable
name="user"
type="com.example.myapplication.SUser" />
<import type="com.example.myapplication.StarUtils"/>
</data>
- 在一级页面中赋值给二级页面
<include
layout="@layout/sub"
//左边的user代表二级页面中的变量,右边的代表一级页面的变量,赋值操作
app:user="@{user}"
/>
3 双向绑定
- 写继承自BaseObservable的类SUser
public class SUser extends BaseObservable{
public String name;
public int lever;
public SUser(String account, int pwd) {
this.name = account;
this.lever = pwd;
}
//当name属性发生变化时,由于调用了notify,所以这个@Bindable会自动被调用
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
if ((name!=null) && (!name.equals(this.name))){
this.name = name;
notifyPropertyChanged(BR.name);
}
}
@Bindable
public int getLever() {
return lever;
}
public void setLever(int lever) {
// if ((lever!=null) && (lever!=this.lever))
this.lever = lever;
notifyPropertyChanged(BR.lever);
}
}
- 在xml文件中写入变量SUser
<data>
<variable
name="user"
type="com.example.myapplication.SUser" />
<import type="com.example.myapplication.StarUtils"/>
</data>
- 在xml文件中使用变量SUser
<EditText
android:text="@={user.name}"
/>
5 . 在mainActivity中使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
db = DataBindingUtil.setContentView(this, R.layout.activity_main);
db.setVm(new UserViewModel());