Android ScrollView 布局问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1526831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:12:27  来源:igfitidea点击:

Android ScrollView layout problem

androidscrollview

提问by Aaron

I have several ListViews in a LinearLayout. It's listing things by day, so I have a TextView containing "Sunday:" followed by a list of items, followed by a "Monday" TextView, etc. Works great, but doesn't fit in the screen. So I added a ScrollView as a parent of the LinearLayout. Now it scrolls, but the ListViews all have room for 2 entries, whether they have 0 or 3 entries. Something about adding the ScrollView parent caused the ListViews to not size dynamically. I tried calling requestLayout() on the ScrollView after the list adapters had filled their views, but that didn't do anything. Any ideas?

我在 LinearLayout 中有几个 ListView。它按天列出事物,所以我有一个包含“Sunday:”的 TextView,然后是项目列表,然后是“Monday”TextView 等。效果很好,但不适合屏幕。所以我添加了一个 ScrollView 作为 LinearLayout 的父级。现在它会滚动,但 ListViews 都有 2 个条目的空间,无论它们是 0 还是 3 个条目。添加 ScrollView 父级的一些事情导致 ListViews 无法动态调整大小。在列表适配器填充视图后,我尝试在 ScrollView 上调用 requestLayout() ,但这没有做任何事情。有任何想法吗?

Edit: From http://www.anddev.org/viewtopic.php?p=25194and other links it seems that ListViews inside a ScrollView are not handled correctly. Anyone have a good suggestion for implementing a list-of-lists?

编辑:从http://www.anddev.org/viewtopic.php?p=25194和其他链接看来,ScrollView 中的 ListViews 没有正确处理。任何人都对实施列表列表有很好的建议吗?

回答by svens

I'm interested in that topic too, so I did a bit of research. First: Never put a ListView in a ScrollView (as you found out yourself). Unfortunately googling this problem doesn't lead to any solutions, so I tried my suggestion from my comment above.

我对这个话题也很感兴趣,所以我做了一些研究。第一:永远不要将 ListView 放在 ScrollView 中(正如您自己发现的那样)。不幸的是,谷歌搜索这个问题并没有找到任何解决方案,所以我尝试了我上面评论中的建议。

I implemented a custom ListAdapter and put the ListViews into one parent ListView. This doesn't work (leads to the same problem as with a ScrollView). Speaking to the guys on the official android-irc #android-dev on freenode, they told me that putting ListViews into a ListView is as bad as or even worse than putting them into a ScrollView. Unfortunately they also couldn't help me with the problem.

我实现了一个自定义 ListAdapter 并将 ListViews 放入一个父 ListView。这不起作用(导致与 ScrollView 相同的问题)。在与 freenode 上的官方 android-irc #android-dev 上的人交谈时,他们告诉我将 ListViews 放入 ListView 与将它们放入 ScrollView 一样糟糕甚至更糟。不幸的是,他们也无法帮助我解决这个问题。

There seems to be only one way to achieve what you want to do; see the answer on this similar question Scrolling with Multiple ListViews for Android. The idea is to merge all ListViews into a single one by a custom adapter and to insert some kind of headers between entries. This is absolutely doable but might require some special effort.

似乎只有一种方法可以实现您想做的事情;请参阅此类似问题的答案Scrolling with Multiple ListViews for Android。这个想法是通过自定义适配器将所有 ListViews 合并为一个,并在条目之间插入某种标题。这是绝对可行的,但可能需要一些特殊的努力。

回答by Kiran Parmar

I know it's late to answer this right now, but still - it may be useful to others who arrive here in case of similar problems.

我知道现在回答这个问题为时已晚,但仍然 - 如果遇到类似问题,它可能对到达这里的其他人有用。

I'd suggest that you use an Expandable ListView for this. It would solve all of your problems. You can have the main/parent names as that of week, and when you expand it, you would have the list of entries for that particular day/week/whatever. Also, you wouldn't have to worry about scrolling as it is taken care by android :)

我建议您为此使用可扩展的 ListView。它会解决你所有的问题。您可以将主要/父名称作为周的名称,当您展开它时,您将拥有该特定日期/周/任何内容的条目列表。此外,您不必担心滚动,因为它是由 android 照顾的 :)

If you DO try this, then please let me know if this works out for your problem Try searching for examples on Expandable ListView.

如果您尝试这样做,请告诉我这是否能解决您的问题 尝试在可扩展 ListView 上搜索示例。

edit:check example here - http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html

编辑:在这里检查示例 - http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html