一、初步尝试JetPack(MVVM架构)

主要用到了 ViewModel、LiveData、DataBinding

  1. 写ViewModel,数据存放
  2. 使用dataBinding 数据双向绑定
    1. gradle添加dataBinding
    2. xml执行绑定

案例:

ViewModel

public class MyViewModel extends ViewModel { 
    private MutableLiveData<Integer> aTeamScore;    // team A score
    private MutableLiveData<Integer> bTeamScore;    // team B score
    private int aBack;  // team A pre score (for undo)
    private int bBack;  // team B pre score (for undo)

    /** * get Team A score * * @return */
    public MutableLiveData<Integer> getaTeamScore() { 
        if (aTeamScore == null) { 
            aTeamScore = new MutableLiveData<>();
            aTeamScore.setValue(0);
        }
        return aTeamScore;
    }

    /** * get Team B score * * @return */
    public MutableLiveData<Integer> getbTeamScore() { 
        if (bTeamScore == null) { 
            bTeamScore = new MutableLiveData<>();
            bTeamScore.setValue(0);
        }
        return bTeamScore;
    }

    /** * add Team A score * * @param p */
    public void aTeamAdd(int p) { 
        aBack = aTeamScore.getValue();
        bBack = bTeamScore.getValue();
        aTeamScore.setValue(aTeamScore.getValue() + p);
    }

    /** * add Team B score * * @param p */
    public void bTeamAdd(int p) { 
        aBack = aTeamScore.getValue();
        bBack = bTeamScore.getValue();
        bTeamScore.setValue(bTeamScore.getValue() + p);
    }

    /** * set score 0 */
    public void reset() { 
        aBack = aTeamScore.getValue();
        bBack = bTeamScore.getValue();
        aTeamScore.setValue(0);
        bTeamScore.setValue(0);
    }

    /** * 撤销操作 */
    public void undo() { 
        aTeamScore.setValue(aBack);
        bTeamScore.setValue(bBack);
    }
}

gradle

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
dataBinding{
    enabled true
}

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="data" type="com.dy.scorejetpack.MyViewModel" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="TeamA" android:textSize="30sp" />
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="TeamB" android:textSize="30sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="50dp">
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="@{String.valueOf(data.getaTeamScore())}" android:textSize="70sp" />
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="@{String.valueOf(data.getbTeamScore())}" android:textSize="70sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="20dp">
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:onClick="@{()->data.aTeamAdd(1)}" android:text="+1" android:textSize="20sp" />
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:onClick="@{()->data.bTeamAdd(1)}" android:text="+1" android:textSize="20sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="20dp">
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:onClick="@{()->data.aTeamAdd(2)}" android:text="+2" android:textSize="20sp" />
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:onClick="@{()->data.bTeamAdd(2)}" android:text="+2" android:textSize="20sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="20dp">
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:onClick="@{()->data.aTeamAdd(3)}" android:text="+3" android:textSize="20sp" />
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:onClick="@{()->data.bTeamAdd(3)}" android:text="+3" android:textSize="20sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="60dp">
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="@{()->data.undo()}" android:src="@drawable/ic_reply_black_24dp" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="@{()->data.reset()}" android:src="@drawable/ic_refresh_black_24dp" />
</LinearLayout>
</LinearLayout>
</layout>

MainActivity

public class MainActivity extends AppCompatActivity { 
MyViewModel mMyViewModel;
ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
mMyViewModel = new ViewModelProvider(this).get(MyViewModel.class);
mBinding.setData(mMyViewModel);
mBinding.setLifecycleOwner(this);
}
}

小技巧:
一些小图标,矢量图Android自带的

本文地址:https://blog.csdn.net/wealth_gold/article/details/110149507