开发者

Is it bad practice to use the LayoutInflater?

Basically I want to populate a ViewFlipper with so开发者_JAVA技巧me views when an AsyncTask has been completed. Rather than hiding the views somewhere in my layout with the visibility set to 'gone', I thought I'd just inflate them from a different layout XML-file on my AsyncTask completion.

Basically the Views just contain some ViewGroups with some text and pictures in them, nothing big. Question is, is this still a bad idea resource-consumption wise?


I'd say it's better, because the activity's initial layout will be smaller (and thus faster to load). The downside is that inflating the view will probably be slightly slower than just making it visible. Note that lists in Android work this way : a few list elements are inflated when needed, and then recycled. Just avoid inflating the same things over and over again.

A nice alternative to this could be using a ViewStub. It does pretty much what you want to do : it doesn't load the layout until you set it's visibility to Visible.

And as for the AsyncTask, be sure to do that work in the onPostExecute method, as it runs on the main UI thread, and you can't manipulate your Views from another thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜