Forgetting is something that most people take for granted and can’t avoid.
However, forgetting in study and work is really a very effective thing, a good memory is better than bad writing, in order to avoid similar situations in the future have to go to a variety of websites to find solutions, but also to have a record of their own study and life, so I decided to write my first blog.
Write an article about the configuration of databinding.
To use databinding, you must first use android{.. gradle in your app’s build.gradle file. } directory to configure these
buildFeatures{
dataBinding = true
viewBinding = true
}
Copy the code
After that, Alt + Enter inserts the databinding structure into the layout header and declares the binding in the Activity or other control file.
The main use of databingding is for bidirectional binding, starting with a variable variable in the corresponding layout XML file, where name is the name and type is the variable type.
<data>
<variable
name="jeeyu"
type="com.example.databindingtest01.MyViewModel" />
</data>
Copy the code
And then, in a little view control like Textview, Button, whatever
android:text="@{String.valueOf(jeeyu.number)}"
Copy the code
Number is the method in the class of the type attribute, and Jeeyu is the name attribute itself
android:onClick="@{()->jeeyu.add()}"
Copy the code
Here, Jeeyu is name, and Add is a method in the class corresponding to Type
Databinding gets the layout and binds the ViewModel to the four most important sentences
binding= DataBindingUtil.setContentView(this,R.layout.activity_main); // Set layout Model =new ViewModelProvider(this).get(myViewModel.class); // Pass the ViewModel class to binding.setjeeyu (model); / / specify the specific value to model the binding. SetLifecycleOwner (this); // If not set, liveData will not workCopy the code