Android - DataBinding - How and when the Binding classes will be generated?

后端 未结 18 1725
小蘑菇
小蘑菇 2021-02-03 16:51

DataBinding Guide States

  By default, a Binding class will be generated based on the name of the layout 
file, converting it to Pascal case and suffixing “Bindi         


        
相关标签:
18条回答
  • 2021-02-03 17:05

    Try to rename layout to main_activity.xml.

    In my case, it didn't work to generate binding class for "activity_main.xml".
    But renaming the layout file to main_activity.xml worked just fine.

    0 讨论(0)
  • 2021-02-03 17:09

    Did you update your layout file for data binding? They are generated only for layouts which have data binding.

    It has to start with a layout tag which has 2 child tags (data & your root view).

    Something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
       <data>
           <variable name="user" type="com.example.User"/>
       </data>
       <LinearLayout ...
       </LinearLayout>
    </layout>
    

    When your layout has this form, AndroidStudio should auto complete the class. Also, by default, it is generated in <your.app.package>.databinding package.

    0 讨论(0)
  • 2021-02-03 17:10

    A weird "bug" in android-studio causes the generated BR.java file not to use the @Bindable fields and still only have the _all property if a res/layout directory doesn't exist.

    It happened to me when I wanted to create a "No Activity" App to have a library that contained my different bindable data.

    0 讨论(0)
  • 2021-02-03 17:11

    If you don't want to restart android studio, one way is to rename activity_main.xml to something else e.g activity_m.xml and then rename it back to activity_main.xml. It is much faster than restarting android studio.

    To rename, right click on activity_main.xml and choose Refactor => Rename

    0 讨论(0)
  • 2021-02-03 17:14

    I ran into a similar issue that hasn't been mentioned here yet. In my case, I incorrectly added <Layout> as the tag versus <layout>. <Layout> is apparently a valid tag because Android Studio didn't complain (other than requiring layout_width and layout_height which aren't required for <layout>). The capitalization difference will manifest in similar behavior where you cannot import the databinding class (import com.example.android.aboutme.databinding.ActivityMainBinding in my case) in the MainActivity class even though <Layout> is a valid tag and the build.gradle file is defined properly to support data binding.

    In my case, I did not need the <data> tags defined yet in order for ActivityMainBinding to validly import and be referenced in:

    private lateinit var binding: ActivityMainBinding

    I was working through the this codelab when I ran into this behavior: https://developer.android.com/codelabs/kotlin-android-training-data-binding-basics/index.html

    0 讨论(0)
  • 2021-02-03 17:16

    What is your layout name?

    The above layout file was activity_main.xml so the generate class was ActivityMainBinding.

    What this means is that the generated class name will depend on your layout's name

    activity_main.xml -> ActivityMainBinding.java

    I think your activity name is "main_activity", so the generated binding class name should be MainActivityBinding not ActivityMainBinding

    0 讨论(0)
提交回复
热议问题