android_ jetpack_ Notes on using data binding in xml layout

Keywords: Android xml jetpack Error

Reprinted from: https://www.jianshu.com/p/b923afeba8c0?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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 "€">    ]
    >
    <resources>
    <string name="xxx">xxx</string>
    </resources>

This.

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:`+(viewmodel.data.dispatcherName??`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

    xmlns:android="http://schemas.android.com/apk/res/android"

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

    android:text="@{viewmodel.count}"

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,

First,

         <variable
            name="adapter"
            type="android.support.v7.widget.RecyclerView.Adapter"/>

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

    app:pullToAdapter="@{adapter}"

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

    @BindingAdapter("adapter")
    public static void setAdapter(RecyclerView view, RecyclerView.Adapter adapter) {
        view.setAdapter(adapter);
    }

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

app:setOnRefreshListener="@{viewModel.onRefreshListener}"

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

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

These are the two methods I know for the time being.

8.tools:text 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