Should I create child textviews statically or dynamically?

前提是你 提交于 2019-12-12 06:38:29

问题


I have a listview where the items contain different amount of child textviews.

min 5
max 20

i've tried both ways and I only have the vm to test my apps on so I can't really tell any difference performance wise.

but what's the best way to do this?

should I create the 20 textviews in my xml and just hide the ones I don't use? or just create and add new textviews everytime, resulting in no "Ghost views"


回答1:


Issues to realise:

  1. Code cleanliness-> Doing anything more than basic layouts in code can get very messy, very fast.

  2. Code re-use-> It’s extremely easy to inflate an XML layout into a specified view with one or two lines of code

  3. code Performance-> Creating the objects necessary for an in-code layout leads to unnecessary garbage collection. As per the Android Designing for Performance article, “avoid creating short-term temporary objects if you can.”

  4. Attribute availability-> Defining Views in an XML layout exposes attributes that are not always available by object methods.

Possible disadvantages:

It make take more time to do an XML layout versus defining the layout in code, especially if there are only one or two interface elements that need to be changed.

After thinking about what I want to accomplish, it makes sense for me to use XML layouts for the dynamic view changes I need. It would be more than just a few lines of code to do my layout changes without utilizing XML layouts.

Now you can decide according to your requirements.



来源:https://stackoverflow.com/questions/8544380/should-i-create-child-textviews-statically-or-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!