4

我正在构建一个递归分层布局。这是我的 message_with_replies.xml (我删除了对齐属性和一些项目以使想法清晰):

<?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>

在 Java 代码中,我使用此布局对项目进行膨胀并将它们嵌入到父级的回复布局中,然后对子级重复此操作,因此它们形成一棵树:

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

当我在顶层或离顶层大约 2-3 层时,一切看起来和执行都很好,但随着我走得更深,布局的响应越来越慢。减速是非线性的,在大约 7 或 8 的深度上,UI 完全卡住了(模拟器在思考了大约半分钟后引发了异常)。

显然,问题在于嵌入式布局。我将线性布局嵌入到相对布局中,因此我的层次结构的每个新级别都意味着在 UI 布局方面有两个额外的嵌入。

如何优化这个?

我可以尝试摆脱线性布局,但我希望这不会解决问题,只会将其推迟到更深层次,因为我仍然会有嵌入。

当我用其他布局膨胀子项目时,通常可以保持层次结构平坦吗?我可以给物品充气,然后在没有容器的情况下取出物品吗?

补充:我已经绕过了这个问题。现在我使用边距构建树,因此不会嵌入布局,并且 UI 不会卡住。一切看起来和工作正常。

但是,原始问题尚未得到解答。

4

1 回答 1

4

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).

As you already seen the problem is the dept of your layout file. Each time you inflate the message_with_replies.xml and add it to the LinearLayout you 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.

How to optimize this?

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 ViewGroups between.

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?

You'll keep the hierarchy flat if you only add Views to the layout. As long as you add ViewGroups to ViewGroups(like you do in your code) you'll increase the hierarchy's dept. An option to avoid adding unnecessary ViewGroups(in some cases) is to use the merge tag(you can find more details here).

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.

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 a RelativeLayout, I don't know your setup as you removed the layout attributes from the layout) with orientation set to vertical. 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 the Button from the layout is set to be on top of the LinearLayout):

<merge>
    <Button android:id="@+id/toggleReplies">
    <!-- content of the replies at this level -->
</merge>

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.

于 2012-09-18T18:58:35.680 回答