一、基础入门
1.在app的build.gradle内添加依赖
dependencies {
.........
implementation "android.arch.lifecycle:extensions:1.1.1"
}
2.创建ViewModel继承AndroidViewModel,
public class MainViewModel extends AndroidViewModel {
private MutableLiveData<User> users;
public MainViewModel(@NonNull Application application) {
super(application);
}
public MutableLiveData<User> getUsers() {
if (users==null){
users=new MutableLiveData<>();
//TODO可以从网络获取
//.......
}
return users;
}
//setValue():须在主线程中才能使用 postValue():既可在主线程也可在子线程中调用 即在工作线程中调用
public void setUsers(String name,String city,int age) {
users.setValue(new User(name,city,age));
}
}
Activity或Fragment内的简单调用(当Activity内包含多个Fragment时,它们可以共用同一个ViewModel,做到数据共享)。
1.在Activity内使用ViewModel
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_model);
final MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
Button button = findViewById(R.id.bt_activity);
final TextView textView = findViewById(R.id.tv_text_activity);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mainViewModel.getUsers().postValue(new User("Activity", "hangzhou", 19));
}
});
//观察数据是否发生变化,当发生变化时就会执行onChanged内的代码程序
mainViewModel.getUsers().observe(this, new Observer<User>() {
@Override
public void onChanged(User user) {
textView.setText("我是来自" + user.getName() + "点击事件,现在在" + user.getCity() + "今年" + user.getAge() + "啦");
}
});
}
2.Fragment内使用ViewModel
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one,container,false);
//此处of内的参数需为getActivity(),不能为this
final MainViewModel mainViewModel = ViewModelProviders.of(getActivity()).get(MainViewModel.class);
Button button = view.findViewById(R.id.bt_one_fragment);
final TextView textView = view.findViewById(R.id.tv_text_one_fragment);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mainViewModel.getUsers().postValue(new User("OneFragment", "shanghai", 29));
}
});
mainViewModel.getUsers().observe(this, new Observer<User>() {
@Override
public void onChanged(User user) {
textView.setText("我是来自" + user.getName() + "点击事件,现在身处" + user.getCity() + "今年" + user.getAge() + "啦");
}
});
return view;
}
二.与DataBinding的结合使用
与DataBinding的单独使用相同,须在XML内导入该ViewModel
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="data"
type="com.example.myapplication.viewmodel.vm.MainViewModel" />
</data>
<TextView
..........
android:text="@{data.users.name}" />
</layout>
之后须在Activity内将DataBinding与ViewModel绑定
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
final ActivityViewModelBinding activityViewModelBinding= DataBindingUtil.setContentView(this,R.layout.activity_view_model);
//将ViewModel与DataBinding绑定
activityViewModelBinding.setData(mainViewModel);
//让xml内绑定的LiveData和Observer建立连接,数据改变,UI自动会更新
activityViewModelBinding.setLifecycleOwner(this);
activityViewModelBinding.btActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { mainViewModel.setUsers("Activity", "hangzhou", 19);
}
});
}
网友评论