Use AndroidX + ViewModel + LiveData + DataBinding component, while using RxJava2 + Retrofit2 + Glide mainstream frame built MVVM

Here Insert Picture Description

Driving is not standardized, two lines of relatives of tears

Overtime is not standardized, two lines of relatives of tears

Families with Suda Jiang, two lines of relatives of tears

TODO-MVVM: JetPack used in AndroidX + ViewModel + LiveData + DataBinding component, while using RxJava2 + Retrofit2 + Glide mainstream framework structures

  • GitHub project address

  • Project basicLibModule-based framework to build can be brought on by, sampleModule project use cases

  • Renderings

2018 Google I / O conference released Jetpack, following details about the Jetpack to him so that we are not unfamiliar:

What Android Jetpack that?

  • Jetpack official website

  • Jetpack video tutorials, science and the Internet

  • Describes the application of official documents

    Android Jetpack is a collection of software components that enable you to more easily develop great Android applications. These components can help you follow best practices, allowing you to get rid of work to write boilerplate code and simplify complex tasks, so you can focus on the desired code.

    Jetpack platform API includes unbundling of androidx. * Package library. This means that it can provide backward compatibility and update frequency higher than the Android platform, in order to ensure that you can always get the latest and best version of Jetpack components.

  • Jetpack including what things?

    Jetpack is divided into four modules are: foundation (Foundation), architecture (Architecture), behavior (Behavior), interface (UI)

Here and have to explain AndroidX, use is still the same as before only change the package name only

  • AndroidX official website

    AndroidX is the original Android support library significant improvement (v7, v4 etc ...)

    For example, prior to com.android.support:appcompat-v7correspond in AndroidX package is androidx.appcompat:appcompat; more packages mapping relationship go to the official website to see Artifact mappings

Ado article mainly talk about Jetpack in architectural components (Architecture)

ViewModel

  • Introduction ViewModel

ViewModel class user interface designed to store and manage data related to the life cycle of the way, change the time to allow data to be stored in the configuration screen rotation when ViewModel class

  • ViewModel life cycle

  • The following is a description using ViewModel + LiveData

    • Layout content

      <?xml version="1.0" encoding="utf-8"?>
      <layout xmlns:android="http://schemas.android.com/apk/res/android">
      
          <data></data>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
      
              <TextView
                  android:id="@+id/tv"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </LinearLayout>
      </layout>
      
    • ViewModel to store data

    public class TestViewModel extends ViewModel {
    
        private MutableLiveData<String> data = new MutableLiveData<>();
    
        public MutableLiveData<String> getData() {
            return data;
        }
    }
    
    • UI monitor data changes the data show
    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<String>() {
                @Override
                public void onChanged(String s) {
                    dataBinding.tv.setText(s);
                }
            });
        }
    }
    

For this written above, it provides a more powerful operation LiveData DataBinding combination with: data can be tied directly to the xml

  • First, change the layout xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="model"
            type="com.azhon.mvvm.news.TestViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{model.data}" />
    </LinearLayout>
</layout>
  • Modify the code at the UI
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);
        //数据改变,UI自动会更新
        dataBinding.setLifecycleOwner(this);
    }
}

ViewModel where there is a magical effect: Activity and Fragment data sharing

  • Use is also very simple: ViewModel when it is created if it is the same Activity/ Fragmentsame instance is returned, the following examples:
  • Activity created ViewModel
LinkageViewModel viewModel = ViewModelProviders.of(this).get(LinkageViewModel.class);
  • Activity in the Fragment create ViewModel
LinkageViewModel viewModel = ViewModelProviders.of(ge tActivity()).get(LinkageViewModel.class);

Of course, there are other things, feel free to -> GitHub project address

Welcome to exchange QQ group together
Here Insert Picture Description

Published 140 original articles · won praise 546 · views 540 000 +

Guess you like

Origin blog.csdn.net/a_zhon/article/details/88947616