[AS2.3.3] MVVM mode learning: DataBinding library, mvvmdatabinding
This is a learning record of mvvm. Most of them are from the internet!
The advantages and disadvantages of Data Binding
Advantages
Before DataBinding appeared, when we implemented the UI interface, we inevitably wrote a large number of unnutritious code: for exampleView. findViewById (); for example, setter for updating View attributes:SetText (),SetVisibility (),SetEnabled () orSetOnClickListener () and so on.
The more "spam code" these are, the more prone to bugs.
With DataBinding, we can avoid writing these "spam codes ".
Disadvantage
Using Data Binding increases the number of classes and methods for compiled apk files.
Create an empty project and open the build. the number of classes and methods in the apk file before and after the Data Binding switch in gradle is increased by more than 120, the number of methods is increased by 9 k + (after obfuscation is enabled, the number is reduced to 3 k + ).
If the project is sensitive to the number of methods, use Data Binding with caution.
1. Basic MVVM usage
Environment matching
The mvvm mode must be implemented only when Android Studio uses the DataBinding library.
Add DataBinding reference to the main app Module of the project
apply plugin: 'com.android.application'android { ...... dataBinding{ enabled = true } ......}
Another note is:
-Android Studio Versions later than 1.3
-The gradle version must be 1.5.0-alpha1 or later.
Basic usage
Before using it, we need to add the outermost layer of the default xml layout to layout nesting.
...... ........
We create the following layout:
-Set text such as TextView EditText Button
Create an mvvmString class first
public class mvvmString { private String str1; private String str2; private String str3; public mvvmString(String str1, String str2, String str3) { this.str1 = str1; this.str2 = str2; this.str3 = str3; } public String getStr1() { return str1; } public void setStr1(String str1) { this.str1 = str1; } public String getStr2() { return str2; } public void setStr2(String str2) { this.str2 = str2; } public String getStr3() { return str3; } public void setStr3(String str3) { this.str3 = str3; }}
Then we set the xml layout.
First, data
Or
Add the variable attribute to data to set the name and type.
Then we can use it. Next we will set the TextView EditText Button
Then we bind and set data to TestActivity.
Public class TestActivity extends AppCompatActivity {private ActivityTestBinding binding; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); binding = DataBindingUtil. setContentView (this, R. layout. activity_test); binding. setString (new mvvmString ("111", "222", "button "));}}
Here, the corresponding Binding will be generated for Binding under which xml
Like thisR. layout. activity_testIts generation is spliced _.ActivityTestBindingThis class
Set images for ImageView
Create an mvvmImage class
public class mvvmImage { @BindingAdapter({"image"}) public static void imageLoader(ImageView imageView, String url){ Glide.with(imageView.getContext()).load(url).into(imageView); }}
Glide is used to load images.
Then we add a new type to data.
LinearLayout and other layout la s are omitted in the middle.
You only need to add the set image to the binding.
String url = "https://7xi8d6.com1.z0.glb.clouddn.com/20180109085038_4A7atU_rakukoo_9_1_2018_8_50_25_276.jpeg";binding.setImage(url);
Set click events for the Button
Or create an mvvmClick class first
Public class mvvmClick {public void onClick (View view) {Toast. makeText (view. getContext (), "click", Toast. LENGTH_SHORT). show ();}}
Then set data
LinearLayout and other layout la s are omitted in the middle.
The usage in the Activity is as follows:
binding.setClick(new mvvmClick());
After you click the button, the Toast will pop up.
In this way, the basic data is bound.
Paste the complete Activity and xml layout code below
Public class TestActivity extends AppCompatActivity {private ActivityTestBinding binding; private String url = "https://7xi8d6.com1.z0.glb.clouddn.com/" + "inline"; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); binding = DataBindingUtil. setContentView (this, R. layout. activity_test); binding. setString (new mvvmString ("111", "222", "button"); binding. setImage (url); binding. setClick (new mvvmClick ());}}
2. Advanced MVVM usage
Use of BaseObservable
Make mvvmStrig inherit BaseObservable and rewrite it
public class mvvmString extends BaseObservable{ private String str1; private String str2; private String str3; public mvvmString(String str1, String str2, String str3) { this.str1 = str1; this.str2 = str2; this.str3 = str3; } @Bindable public String getStr1() { return str1; } public void setStr1(String str1) { this.str1 = str1; notifyPropertyChanged(com.gjn.msdemo.BR.str1); } @Bindable public String getStr2() { return str2; } public void setStr2(String str2) { this.str2 = str2; notifyPropertyChanged(com.gjn.msdemo.BR.str2); } @Bindable public String getStr3() { return str3; } public void setStr3(String str3) { this.str3 = str3; notifyPropertyChanged(com.gjn.msdemo.BR.str3); }}
Bindabel the get data. Call the refresh method notifyPropertyChanged after setting the properties.
At this time, you only need to modify the data
MvvmString string = new mvvmString ("111", "222", "button"); binding. setString (string); string. setStr1 ("333"); string. setStr2 ("444"); string. setStr3 ("button 2 ");
You can modify it. Otherwise, if BaseObservable is not inherited, it is required to modify the data.
Follow these steps:
MvvmString string = new mvvmString ("111", "222", "button"); binding. setString (string); string. setStr1 ("333"); string. setStr2 ("444"); string. setStr3 ("button 2"); binding. setString (string );
It is re-bound. BaseObservable only adds refresh.
However, the BaseObservable method is too troublesome. If there are more classes. Each class writes an M binding... Really tiring
-Use of ObservableField
Create a new mvvmString2
public class mvvmString2 { public final ObservableField str1 = new ObservableField<>(); public final ObservableField str2 = new ObservableField<>(); public final ObservableField str3 = new ObservableField<>();}
Modify data usage
Use the following
mvvmString2 string2 = new mvvmString2(); binding.setString(string2); string2.str1.set("text"); string2.str2.set("edit"); string2.str3.set("button");
It is convenient to create M and quick to set.
-BindingAdapter usage
The BindingAdapter is used when the image is modified. Here we will talk about the usage of the BindingAdapter.
First, create an xml layout.
Act_test_adapter.xml
We also created a new mvvmAdapter class.
public class mvvmAdapter { public ObservableArrayList texts = new ObservableArrayList<>(); public mvvmAdapter(){ for (int i = 0; i < 5; i++) { texts.add("text => " + i); } } @BindingAdapter({"texts"}) public static void addTextView(LinearLayout linearLayout, ArrayList texts){ linearLayout.removeAllViews(); for (String text : texts) { TextView textView = new TextView(linearLayout.getContext()); textView.setText(text); linearLayout.addView(textView); } } public void add(View v){ texts.add("new text"); }}
Modify TestActivity
public class TestActivity extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActTestAdapterBinding binding = DataBindingUtil.setContentView(this,R.layout.act_test_adapter); binding.setAdapter(new mvvmAdapter()); }}
The implementation result is as follows:
Other Instructions
Custom Binding name
First, The Bingding we created is generated according to xml. If we do not want to generate it like this, we can modify the data and add the class attribute to it.
For example
Alias
You can set an alias when using the import package, which helps to distinguish some packages with similar names, that is, adding the attribute alias to the import
Add a package when setting properties
If you want to set some attributes
For example, if you want to set the display attribute of a view
Slightly modify the above mvvmAdapter. Paste the modified code here
MvvmAdapter public class mvvmAdapter {public final ObservableField IsView = new ObservableField <> ();} xml Activitypublic class TestActivity extends AppCompatActivity {@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); // ActTestAdapterBinding binding = DataBindingUtil. setContentView (this, R. layout. act_test_adapter); NewBinding binding = DataBindingUtil. setContentView (this, R. layout. act_test_adapter); mvvmAdapter adapter = new mvvmAdapter (); binding. setAdapter (adapter); adapter. isView. set (false );}}