Skip to content Skip to sidebar Skip to footer

In Android, What Layout Is Faster 1. Framelayouts Inside Linearlayout Or 2. One Big Relativelayout?

I am considering two different layout arrangements for a view (which will be a row in a RecyclerView). They are different, but they will both work, either as One container vertica

Solution 1:

Short Answer (TLDR):

If you really know what you're doing, RelativeLayouts can be faster. If you don't, they can be much slower.

Long Answer:

Nesting layouts manually such as a FrameLayout within a LinearLayout versus using relative positioning in a RelativeLayout can have pros and cons either way.

The cost is in calculating the dynamic sizes such as "wrap_content" which must expand based on their children's needs or constrain due to parent's requirements. This causes a factorial calculation problem which increases with depth.

Manual positioning by using nested views increases levels. The shallower the level, the better but still work. So literally the amount of work is the factorial of your level depth as described above. For example: A is nested under Root. B is nested under A. C is nested under B. A affects Root's width. B affects A which affects Root. C affects B which affects A which affects Root.

Relative positioning by having one view position itself relative to another is the same as nested views in terms of dynamic sizing calculations since each relationship must be calculated with consideration of the other. For example:

A is left of Root. B is left of A. C is left of B. While A,B,C all live under Root, the dynamic measurement occurs like so: A affects Root's width. B affects A which affects Root. C affects B which affects A which affects Root.

In other words, you have the same factorial calculation occurring.

So the only real difference is that RelativeLayouts give you finer control over relative positions at the cost of more complex XML.

On the flip side, with careful use of @dimens that are calculated ahead of time based on device attributes, the dynamic calculations can be avoided and when used properly, RelativeLayouts can be much more performant than any other layout when needing complex positioning.

Solution 2:

It depends on the context, there's no easy way to answer this question. There are existing threads with similar discussions already, and it is thought that performance gain is negligible.

RecyclerView allows you to reuse existing ViewHolders. A limited amount of ViewHolders ("RecyclerView pool") is created once, with different view type count in mind.

Assuming you do not spoil ViewHolder pattern, by, e.g. inflating views each time in bind method, you are unlikely to feel the difference between RelativeLayout and LinearLayout root layouts.

However, if you use some sort of objects with dynamic size properties (e.g. ImageView with height set to wrap_content and you load different bitmaps into it), it is likely that your layout will be invalidated and calculated again on each call to onBindViewHolder().

So my assumption is, reasonable usage of RelativeLayout won't bring you into any performance trouble.

Post a Comment for "In Android, What Layout Is Faster 1. Framelayouts Inside Linearlayout Or 2. One Big Relativelayout?"