如何优化递归嵌入式布局? [英] How to optimize the recursively embedded layout?

查看:181
本文介绍了如何优化递归嵌入式布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个递归的分层布局。这是我的message_with_replies.xml(我已经删除对齐属性和一些项目作出明确的想法):

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>
    <按钮
        机器人:ID =@ + ID / toggleR​​eplies
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@绘制/ button_expand_replies/>
    <的LinearLayout
        机器人:ID =@ + ID /回复
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>
    < / LinearLayout中>
< / RelativeLayout的>

在Java的code,我吹的物品用这个布局,并嵌入到了的回复的母公司布局,然后与孩子们重复这一点,所以他们形成了一个树:

 根项目
- 根的孩子0
- - 根儿童0儿童0
- - 根儿童0儿童1
- - - 根儿童0儿童1儿童0
- 根的孩子1
- 根的孩子2
- 根的孩子3
- - 根儿童3儿童0

一切看起来,当我在顶层或从顶部约2-3个级别进行罚款,但因为我去更深,布局响应慢。经济放缓是非线性的,并在约7或8的UI完全stucks深度(模拟器思想约半分钟后抛出一个例外)。

显然,该问题被嵌入布局。我嵌入的线性布局进入相对的,所以我的层次结构的每个新的水平意味着两个附加嵌入在UI布局的条款。

如何优化呢?

我可以尝试摆脱线性布局,但我预计不会解决问题,但只会推迟它的一些更深层次的,因为我仍然有嵌入。

时一般可以保持层次扁平,当我吹一个分项与ahother布局?我可以膨胀的项目,然后利用其内容没有他们的容器?

补充:我绕过这个问题。现在,我用建利润率我的树,所以布局不嵌入,以及用户界面不会卡住。一切看起来和正常工作。

但原来的问题还没有回答。


解决方案

  

一切看起来,当我在顶层或有关执行罚款
  从顶部2-3平,但我更深入,布局响应
  慢。减速是非线性的,并在深度
  约7或8的UI完全stucks(模拟器引发
  想了半分钟左右)后除外。


正如你已经看到的问题是,你的布局文件的部门。每次充气 message_with_replies.xml ,并把它添加到的LinearLayout 你增加你的布局由2部(这将降低性能)。所以,你的时间不少,很多得到你早就超过了推荐的最大值布局部门的7-8级(10,如果我没有记错的话)。


  

如何优化呢?


如果您希望继续在旧视图膨胀的布局则没有太多的事情。否则,你将添加视图总纲没有额外的 ViewGroups 之间。


  

时一般可以保持层次扁平,当我一个充气
  分项与ahother布局?我可以抬高项目,然后利用其
  内容没有他们的容器?


您会保持层级扁平,如果你只添加查看的布局。只要你加入 ViewGroups ViewGroups (如你在code做),你会增加层次的部门。避免增加一个选项,不需要的 ViewGroups (在某些情况下)是使用合并标签(你可以找到更多的细节这里)。


  

补充:我已经绕过这个问题。现在,我使用的利润率打造我的树,
  所以布局不嵌入,以及用户界面不会卡住。一切
  看起来和正常工作。


这应该是你的第一选择,而不是一次又一次膨胀的布局,因为它是一个非常简单的解决方案。布局可以通过使用一个简单的的LinearLayout (或 RelativeLayout的,我不知道你的设置,你可以得到删除布局从布局属性)与定位设置为垂直。这个布局将添加所需的意见,同时考虑基础上添加你想要这个孩子是水平的利润率。如果你想更简化的东西,你可以创建类似下面的布局文件(这个工程如果按钮从布局设置为上的<$ C $顶部C>的LinearLayout )

 &LT;合并&GT;
    &LT;按钮机器人:ID =@ + ID / toggleR​​eplies&GT;
    &LT;! - 在这个级别的答复内容 - &GT;
&LT; /合并&GT;

您夸大此布局后,您需要设置填充(X填充为第一级,2个第二级,3为第三级等),然后将其添加到的LinearLayout

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.

解决方案

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.

这篇关于如何优化递归嵌入式布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆