Android data binding vs findViewById for performance

前端 未结 2 1732
-上瘾入骨i
-上瘾入骨i 2020-12-20 12:07

I realize the potential for android data binding for MVVM is great and it opens doors. But regarding performance, is data binding faster then using findViewById in native

相关标签:
2条回答
  • 2020-12-20 12:23

    It looks to me that it is faster per the docs:

    Views With IDs

    A public final field will be generated for each View with an ID in the layout. The binding does a single pass on the View hierarchy, extracting the Views with IDs. This mechanism can be faster than calling findViewById for several Views.

    So it seems data binding only has to do a single pass over the view hierarchy instead of us always calling findViewById.

    0 讨论(0)
  • 2020-12-20 12:44

    Databinding is really faster compared to findViewById and setText . Not only performance, It is also much faster and maintainable for mid, full-scale projects. As a side note, benefits include:

    • performance
    • logic extraction from ui
    • code minimalization using livedata
    • testability
    • readability
    • avoiding null pointer exception when view is null(I love this feature)
    • custom property declaration in xml
    • updating multiple properties simultaneously

    In a nutshell, Databinding is a really cool feature. To learn about these features, you can visit codelabs here: https://codelabs.developers.google.com/codelabs/android-databinding/#0

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