简体   繁体   English

Android使用哪个ViewGroup

[英]Android which ViewGroup to use

This is a very specific case so I'll try my best to organize this question appropriately. 这是一个非常具体的案例,因此我将尽力适当地组织这个问题。 I have a custom ViewGroup that is working for my purpose but it's initializing is causing skipped frames. 我有一个自定义的ViewGroup ,它可用于我的目的,但初始化会导致跳帧。

My app contains a ViewPager of Fragment 's where the parent container to each is a ScrollView . 我的应用程序包含一个FragmentViewPager ,其中每个容器的父容器是ScrollView Within these, I have many different views, one being a LinearLayout containing vertical rows of custom views (to manipulate a data set). 在这些视图中,我有许多不同的视图,其中一个是LinearLayout ,其中包含自定义视图的垂直行(用于操作数据集)。 See in picture 'Foreground, Visible, Secondary apps' 参见图片“前景,可见,辅助应用程序”

For this layout, I've extended LinearLayout and each one is populated at runtime, inflating each child from xml. 对于此布局,我扩展了LinearLayout并在运行时填充了每个布局,从xml扩展了每个子级。

It's working fine, the issue is that inflating all these child views at runtime is causing skipped frames. 一切正常,问题在于在运行时夸大所有这些子视图会导致跳帧。 I've used method tracing to find that each call to LayoutInflater.inflate() is taking avg 4-6 ms. 我使用方法跟踪发现对LayoutInflater.inflate()每次调用平均花费4-6毫秒。 Between all my fragments each containing 1 or more of these lists, it's adding up to a significant amount (40-50) skipped frames on startup. 在我的所有片段中,每个片段包含1个或多个这些列表,在启动时,它们总共累加了(40-50)个跳过的帧。

Manually creating child views instead of inflating improved it a little, but not enough. 手动创建子视图而不是夸大它可以改善一点,但还不够。 Still very noticeable frame skipping. 跳帧还是很明显的。

It needs some form of view re-using, similarly to what a ListView does. 它需要某种形式的视图重用,与ListView相似。

I see one solution although it doesn't seem ideal at all. 我看到了一个解决方案,尽管它似乎根本不理想。 That is to replace the fragment container ScrollView with a ListView with multiple view types. 那就是用具有多种视图类型的ListView替换片段容器ScrollView

Some problems I see with this are 我看到的一些问题是

  • It would require up to 5-6 different view types between the views above, below, and between these lists. 在上方,下方的视图以及这些列表之间的视图,最多需要5-6种不同的视图类型。 Seems very messy to write. 看起来很乱。
  • Each child view needs the ability to expand size vertically with a simple expand/collapse animation. 每个子视图都需要具有通过简单的扩展/折叠动画垂直扩展尺寸的功能。 Not sure if this is even possible to do with ListView rows. 不确定是否可以使用ListView行。
  • It involves rewritting almost the entire app. 它涉及重写几乎整个应用程序。 Which I am willing to do but would like to avoid if possible. 我愿意这样做,但如果可能的话,我想避免这样做。

I wondered is it at all possible to do: 我想知道是否有可能做到:

  • Instead of using LinearLayout for these lists, use a ListView and have its size be expanded to show all items. 不要将LinearLayout用于这些列表,而应使用ListView并扩大其大小以显示所有项目。 It would be nested in the parent ScrollView . 它将嵌套在父ScrollView Would it even reuse any views if its size was expanded to all items? 如果将其大小扩展到所有项目,它甚至会重用任何视图吗?

Is there any other type of container I can use in this circumstance? 在这种情况下我还能使用其他类型的容器吗? I have not used the new RecyclerView yet but from what I've read it seems essentially the same as a ListView . 我还没有使用过新的RecyclerView ,但是从我阅读的内容来看,它似乎与ListView基本上相同。

Basically what I'm trying to create is a LinearLayout that recycles views. 基本上,我要创建的是一个回收视图的LinearLayout

Would appreciate any suggestions. 将不胜感激任何建议。 Please let me know if this question doesn't read well I can add more information or post code if needed. 如果这个问题听不清,请告诉我,如果需要,我可以添加更多信息或邮政编码。 I realize what I'm trying to do here is a bit unconventional and may be inherently flawed by design. 我意识到我在这里想要做的事情有点不合常规,可能会因设计而固有地存在缺陷。 But i'm very pleased with how it's presented currently and would like to keep it this way. 但是我对它现在的呈现方式感到非常满意,并希望保持这种方式。

From what I understand you have clickable title views that when clicked an inner layout is shown/hidden. 据我了解,您具有可单击的标题视图,单击该视图可显示/隐藏内部布局。

So you could have your view inflate without the inner layouts.Just the clickable titles. 这样一来,您无需内部布局就可以使视图膨胀。 Then when the titles are clicked, if the corresponding inner layout hasn't been inflated yet then you inflate it. 然后,在单击标题时,如果尚未放大相应的内部布局,则可以对其进行充气。 Otherwise you show/hide it. 否则,您将显示/隐藏它。 That should save up a lot of time from the initial inflation. 从最初的通货膨胀中,这应该节省很多时间。 Also building your layout programmatically on runtime instead of inflating an xml saves a lot of time too because you don't have to use findViewById() method that is quite costly. 同样,在运行时以编程方式构建布局而不是使xml膨胀也可以节省大量时间,因为您不必使用成本很高的findViewById()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM