android_ jetpack_ Notes on using data binding in xml layout

Keywords: Android xml jetpack Error

Reprinted from:

Notes on the use of databinding

1. Space.

Using spaces directly in xml will report errors.

        MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence. 

You need to declare a string in the xml and add it to the header

    <!DOCTYPE resources [ 
       <!ENTITY nbsp " ">  
      <!ENTITY copy "©">  
      <!ENTITY reg "®">   
     <!ENTITY trade "™">   
     <!ENTITY mdash "—">  
      <!ENTITY ldquo """>  
      <!ENTITY rdquo """>   
     <!ENTITY pound "£">  
      <!ENTITY yen "¥">  
      <!ENTITY euro "€">    ]
    <string name="xxx">xxx</string>


2. Colon

It could have been used. However, be careful not to mix full width and half width.

3. Air judgment

If you want to use space judgment, you can use xx??xxx is like this, but note that if you want to splice strings, you need to enclose the following (); as

    android:text="@{`dispatcher:`+(`To be assigned`)}"

4. The root layout is added but the corresponding binding is not generated?

Just put the definition of android in the layout


5. Some things of type int cannot be used directly. For example, the count here is int


The system will think that we have passed in a resource. No meeting report found

    <font color = "red">android.content.res.Resources$NotFoundException</font>

Therefore, you need to splice an empty character to become a non int type

6. For fields that you want to update dynamically, you can use

  1. ObservableInt, ObservableField, etc. when changing, user.firstName.set("Google"); The value can be updated.
  2. Or POJO inherits BaseObservable, then uses @ Bindable annotation on get method, and set method calls notifyPropertyChanged(BR.firstName);

7. we know that there are many common methods, such as recyclerview's setadapter method, which can get RecyclerView objects through Binding in code, and then call setadapter method. This is a method often used before without databinding. There are other schemes in databinding,



Define an adapter in xml, and then set the xml settings of your RecyclerView through binding settings


So how did the pullToAdapter come from. You can define a separate class to handle similar methods through @ BindingAdapter

    public static void setAdapter(RecyclerView view, RecyclerView.Adapter adapter) {

Or there is another method. For example, we need to set the drop-down refresh on the RecyclerView to load more.


Write getOnrefreshListener or onRefreshListener in the ViewModel (get can be omitted),

     public PullToRefreshBase.OnRefreshListener getOnRefreshListener() {
            return refreshView -> {
                if (mCurrentPage == mTotalPage) {
           -> mView.getFooterView().completeSetText(mBinding.selectStationName));
                } else {
                    getStations(AreaId + "", mStationType + "", mPage);

These are the two methods I know for the time being. and other properties in layout preview cannot be used.

Just upgrade to AS3.0.

9 ?? Use of

If you want to splice?? You need to enclose it when you need it
such as

"Time" + data.time?? "Unknown"

In this way, even if data.time is null, it cannot be unknown, because it uses "time null" to judge null
So it needs to be changed to

"Time" + (data.time? "Unknown")

Posted by segwaypirate on Wed, 13 Oct 2021 08:36:29 -0700