1. 添加绑定设置
如上一篇文章(Android - DataBinding)一样,在 build.gradle 中添加
buildFeatures{
dataBinding true
}
2. 添加ViewModel类
MyViewModel.java :
public class MyViewModel extends ViewModel {
private MutableLiveData<Integer> mATeamScore;
private MutableLiveData<Integer> mBTeamScore;
private int mATeamLastScore;
private int mBTeamLastScore;
public MutableLiveData<Integer> getATeamScore() {
if (mATeamScore == null) {
mATeamScore = new MutableLiveData<>();
mATeamScore.setValue(0);
}
return mATeamScore;
}
public MutableLiveData<Integer> getBTeamScore() {
if (mBTeamScore == null) {
mBTeamScore = new MutableLiveData<>();
mBTeamScore.setValue(0);
}
return mBTeamScore;
}
public void addScoreToA(int i) {
saveLastScore();
mATeamScore.setValue(mATeamScore.getValue() + i);
}
public void addScoreToB(int i) {
saveLastScore();
mBTeamScore.setValue(mBTeamScore.getValue() + i);
}
public void resetScore() {
mATeamScore.setValue(0);
mBTeamScore.setValue(0);
}
public void undoScore() {
mATeamScore.setValue(mATeamLastScore);
mBTeamScore.setValue(mBTeamLastScore);
}
private void saveLastScore() {
mATeamLastScore = mATeamScore.getValue();
mBTeamLastScore = mBTeamScore.getValue();
}
}
3. activity_main.xml添加绑定
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="myViewModel"
type="com.example.databindingviewmodellivedata.MyViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Team A"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@{String.valueOf(myViewModel.getATeamScore())}"
android:textSize="50sp"
android:textColor="#2196F3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="+1"
android:onClick="@{()->myViewModel.addScoreToA(1)}"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="+2"
android:onClick="@{()->myViewModel.addScoreToA(2)}"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="+3"
android:onClick="@{()->myViewModel.addScoreToA(3)}"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:text="退一步"
android:onClick="@{()->myViewModel.undoScore()}"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Team B"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="@{String.valueOf(myViewModel.getBTeamScore())}"
android:textSize="50sp"
android:textColor="#E91E63" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="+1"
android:onClick="@{()->myViewModel.addScoreToB(1)}"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="+2"
android:onClick="@{()->myViewModel.addScoreToB(2)}"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:text="+3"
android:onClick="@{()->myViewModel.addScoreToB(3)}"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:text="重置"
android:onClick="@{()->myViewModel.resetScore()}"/>
</LinearLayout>
</LinearLayout>
</layout>
4. MainActivity.java添加绑定
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
MyViewModel myViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
activityMainBinding.setMyViewModel(myViewModel);
activityMainBinding.setLifecycleOwner(this);
}
}
5. 运行效果
6. 注意事项
7. 示例代码
https://gitee.com/jie-xio/android_samples/tree/master/DataBinding_ViewModel_LiveData/DataBindingViewModelLiveData
|