samedi 9 mai 2015

ViewStub to use or not (changing inflate id in runtime)

I have one some questions about ViewStub and performance in Android.

Let's assume that there three different types of views : Circle, Rectangle and Square views. I have custom compound view, and one of these three types should be added to the view hierarchy depending on attribute of custom compound view. So in this case I have only one way to do this - programmatically. As far as we need to add view in runtime there are several ways to do this.

  1. Just read attribute value, inflate one of three views and simply add it to the view hierarchy(with setting some attributes of this view).

  2. Another way is to have ViewStub in our compound view in and in this case we have different xml files with declaration of the three views (circle,square,rect). Because we need to have id to pass to the view stub. And depending on attribute value of compound view in runtime setInflateResourceId and inflate view. This method returns us inflated view and we can cast it to appropriate view and do some stuff with this view.

What method is the most correct and has better performance ? Or maybe there is another way to implement this.

Thank you.

Aucun commentaire:

Enregistrer un commentaire