Todo-mvvm: Built using AndroidX + ViewModel + LiveData + DataBinding components in JetPack and mainstream frameworks like RxJava2 + Retrofit2 + Glide

  • GitHub project address

  • The basicLibModule is the basic framework for the project, and the sampleModule is the use case for the project

  • rendering

Here’s a closer look at Jetpack, which was released at Google I/O 2018:

What is Android Jetpack?

  • Jetpack website

  • Jetpack video tutorial, science Online

  • An introduction to the official application documentation

    Jetpack is a collection of Android software components that make it easier for you to develop great Android applications. These components help you follow best practices, take the work out of writing boilerplate code and simplify complex tasks so you can focus on the code you need.

    Jetpack contains the Androidx.* package library unbundled with the platform API. This means that it provides backward compatibility and updates more frequently than the Android platform, ensuring that you always get the latest and greatest version of the Jetpack component.

  • What does Jetpack include?

    Jetpack is divided into four modules: Foundation, Architecture, Behavior and UI.

It’s also worth noting that AndroidX works exactly the same as before with a package name change

  • AndroidX website introduction

    AndroidX is a major improvement over the original Android support library (V7, V4, etc……)

    Before such as com. Android. Support: appcompat – v7 corresponding package for AndroidX in AndroidX. Appcompat: appcompat; For more package mappings visit the Artifact Mappings website

Without further ado, this article is going to focus on the Architecture component in Jetpack.

ViewModel

  • Introduce the ViewModel

The ViewModel class is designed to store and manage the lifecycle of data related to the user interface, allowing data to be saved when the screen rotation configuration changes in the ViewModel class

  • ViewModel lifecycle

  • The following is an introduction to using ViewModel + LiveData

    • Layout of the content

    • ViewModel stores data

    public class TestViewModel extends ViewModel {
    
        private MutableLiveData data = new MutableLiveData<>();
    
        public MutableLiveData getData(a) {
            returndata; }}Copy the code
    • The UI listens for data changes and displays the data
    public class Test extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final ActivityTestBinding dataBinding = DataBindingUtil.setContentView(this,
                    R.layout.activity_test);
    
            TestViewModel model = ViewModelProviders.of(this).get(TestViewModel.class);
            model.getData().observe(this.new Observer() {
                @Override
                public void onChanged(String s) { dataBinding.tv.setText(s); }}); }}Copy the code

For this kind of writing, The combination of LiveData and DataBinding provides an even more powerful operation: the data can be directly bound to XML

  • Start by changing the XML layout

  • Modify the UI code

public class Test extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActivityTestBinding dataBinding = DataBindingUtil.setContentView(this,
                R.layout.activity_test);

        TestViewModel model = ViewModelProviders.of(this).get(TestViewModel.class);
        dataBinding.setModel(model);
        // The UI is automatically updated when data changes
        dataBinding.setLifecycleOwner(this); }}Copy the code

Another useful aspect of ViewModel is sharing data between activities and fragments

  • ViewModel returns the same instance of an Activity or Fragment when created, as shown in the following example:

  • Create the ViewModel in the Activity

    LinkageViewModel viewModel = ViewModelProviders.of(this).get(LinkageViewModel.class);
    Copy the code
  • The Fragment in the Activity creates the ViewModel

    LinkageViewModel viewModel = ViewModelProviders.of(ge tActivity(a)).get(LinkageViewModel.class);
    Copy the code

    And of course a few other things, welcome to — >GitHub project address