Android uses databinding to set the null state
Write in front
During normal development, we need to show the data loading:
There are many ways to set the page status. Recently, the author has been using data binding, and the way of data binding to display the view by changing the model is also fully consistent with the setting of the status page.
So let's talk about using data binding to set various status pages in Android. It's very simple. Let's see the effect first
first
Open databinding in the build.gradle file of the app
Let's first define some annotations emptystate for state
Another custom exception, emptyexception, is used to display the status information we need
Now, most controls that display status pages provide
So our goal is to show these
layout
Layout in the form of data binding, and use statemodel to control the messages displayed on the status page
There are several methods in the layout file
We define the ViewModel of the state, called statemodel, to control the state
A very common view model mainly has several methods for judging the status display
The practical application is as follows:
In the activity or fragment layout, add the layout of the status page
Finally, in activity or fragment, we can easily set various states through state. Bindthrowable() and state. Setemptystate() methods.
Write at the end
For those who want to use data to control the view state, using data binding is really a way to get twice the result with half the effort. And it's easy to understand.
Last demo address: statebinding_ jb51.rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.