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

后端 未结 18 1745
小蘑菇
小蘑菇 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:20

    Rule

    Layout name is in snake_case, and generated binding class name will be PascalCase.

    If you layout name is activity_home.xml then binding class name will be ActivityHomeBinding.class.

    Problem

    1. Many times you don't get import suggestion of DataBinding class.
    2. Binding class is not generated when there is some issue in layout.
    3. Binding class are not available when build is failed

    Here is hack

    When you Don't get Import Suggestion

    • When you don't get import suggestion. Import manually your binding class like this. (IDE often does not show suggestions for binding classes)

      import databinding.ActivityMainBinding;
      

    Import line still shows error?

    • If your import line shows error, then try make project (ctrl + F9) / Build> Make Project. .
      1. If Build is failed due to some error, then solve it.
      2. If build is successful then binding class will be generated.

    Quick hack for generating binding class-

    • If your binding class not generated then close project (File > Close Project) and open from recent.

    Note that I recommend close and open from recent because it takes less time than Rebuild / Restart IDE.

    Quick hack for generating layout variables in binding class-

    • If your layout data variable are not generated then close project (File > Close Project) and open from recent.

    If you still have issues. Let me know in comments, or see this answer for better understanding.

提交回复
热议问题