I am building a recursive hierarchical layout. Here is my message_with_replies.xml (I’ve removed the alignment attributes and some items to make the idea clear):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/toggleReplies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_expand_replies" />
<LinearLayout
android:id="@+id/replies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
In Java code, I inflate the items with this layout and embed them into the replies layout of the parent, and then repeat this with the children, so they form a tree:
root item
- child 0 of root
- - child 0 of child 0 of root
- - child 1 of child 0 of root
- - - child 0 of child 1 of child 0 of root
- child 1 of root
- child 2 of root
- child 3 of root
- - child 0 of child 3 of root
Everything looks and performs fine when I’m on the top level or about 2-3 levels from the top, but as I go deeper, the layout responds slower and slower. The slowdown is non-linear, and on the depth of about 7 or 8 the UI completely stucks (the emulator raises an exception after thinking for about half a minute).
Obviously, the problem is embedded layouts. I embed a linear layout into the relative one, so each new level of my hierarchy means two additional embeddings in terms of UI layouts.
How to optimize this?
I could try to get rid of the linear layout, but I expect that will not solve the problem but will only postpone it to some deeper level because I will still have the embedding.
Is it generally possible to keep the hierarchy flat when I inflate a sub-item with ahother layout? Can I inflate the item and then take its contents without their container?
Added: I’ve bypassed the problem. Now I build my tree using margins, so the layouts are not embedded, and the UI doesn’t stuck. Everything looks and works fine.
However the original question is not yet answered.
As you already seen the problem is the dept of your layout file. Each time you inflate the
message_with_replies.xmland add it to theLinearLayoutyou increase the dept of your layout by 2(which will reduce the performance). So, by the time you get to the 7-8 level you would have already exceeded the recommended maximum layout dept(10, if I’m not mistaken) by quite a lot.If you want to continue to inflate the layout in an old view then there isn’t much to do. Otherwise you would add the views to a master layout without the extra
ViewGroupsbetween.You’ll keep the hierarchy flat if you only add
Viewsto the layout. As long as you addViewGroupstoViewGroups(like you do in your code) you’ll increase the hierarchy’s dept. An option to avoid adding unnecessaryViewGroups(in some cases) is to use themergetag(you can find more details here).This should have been your first option instead of inflating the layout again and again as it is a much simpler solution. The layout could be obtained by using a simple
LinearLayout(or aRelativeLayout, I don’t know your setup as you removed the layout attributes from the layout) withorientationset tovertical. Into this layout you would add the desired views, taking in consideration to add the margin based on the level you want this child to be. If you want to simplify things even more you would create a layout file like below(this works if theButtonfrom the layout is set to be on top of theLinearLayout):After you inflate this layout you would need to set the padding(x padding for the first level, 2x for the second level, 3x for the third level etc) and then add it to the
LinearLayout.