Android动态计算列数

发布于 2024-12-20 15:17:03 字数 364 浏览 1 评论 0 原文

我有一个动态创建的项目列表,该列表应显示在两列或一列中,具体取决于包含文本所需的空间,例如,如果文本很长,则每行只有一个项目,否则为两个。显然它们的尺寸应该相同(屏幕尺寸的一半)。

据我所知,没有标准的 Android 视图和 Adapter 可以做到这一点。使用 GridView,您可以拥有多个列,但不能有一些行一列,另一些行两列。 TableLayout 可以拉伸视图,但在这里您还必须知道每行需要多少列。另外它没有适配器。

所以我想知道a)我是否缺少任何支持类似功能的控件,或者b)解决这个问题的最简单的解决方案是什么?

编辑:这些项目还包含一个复选框,我需要跟踪选中状态,因此我不能只使用适配器将两个项目放入一个视图中。

I have a dynamically created list of items which should be displayed in either two or one column depending on the space the containing text needs e.g. if the text is long it will only be one item per row, otherwise two. Obviously they should all be the same size (half the screen size).

As far as I know there's no standard Android view with Adapter that does that. With a GridView you can have multiple columns, but not some rows one column and others two. A TableLayout could stretch views, but also here you have to know how many columns you need per row. Plus it doesn't have an Adapter.

So what I want to know a) is there any control that I'm missing that supports something like this or b) what would be the easiest solution for this problem?

EDIT: the items also contain a CheckBox and I need to keep track of the checked state so I can't just put two items in one view using an Adapter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残疾 2024-12-27 15:17:03

我想说,由于将数据映射到项目到行的问题,这是使用标准组件解决的一个难题。

例如,如果您使用具有 x 行的 Cursor 向适配器提供数据,则从 Adapter 中看到的总项目数也是x。但是,由于您有条件地将两个项目映射到同一行,这意味着 ListView 将在 Adapter 中看到 y 行,其中y <= x。但你不能轻易地从一开始就知道 y 是什么。此外,如果 ListViewAdapter 请求项目 i,其中 0 <= i << y,Adapter 没有(简单)方法可以确定 i 将映射到 Cursor 中的哪些元素。

话虽这么说,一个可行的解决方案是子类化 AdapterViewListView 并自己实现元素的布局。当您从 Adapter 获取每个项目时,您将根据周围适配器项目的大小对其进行测量和布局。

如果您没有大量元素,另一种可能适合您的解决方案是使用两个自定义 Adapter,一个称为 ItemAdapter,另一个称为 行适配器。 ItemAdapter 将根据(假定的)Cursor 来扩充实际项目。 RowAdapter 将使用 ItemAdapter 获取项目并将它们合并到行中。 ListView 将依次使用 RowAdapter。问题是,要知道 RowAdapter 将生成多少行,有必要在连接 RowAdapter 之前测量 ItemAdapter 中的所有项目到ListView

I'd say that this is a hard problem to solve using standard components, due to the problem of mapping data to items to rows.

If you for example use a Cursor with x rows to feed the adapter with data, then the total item count as seen from the Adapter is also x. However, since you're conditionally mapping two items to the same row, it means that a ListView will see y rows in the Adapter, where y <= x. But you cannot easily tell from the beginning what y will be. Furthermore, if the ListView asks the Adapter for item i where 0 <= i < y, there would be no (easy) way for the Adapter to determine which elements from the Cursor that i would map to.

That being said, a viable solution would be to subclass AdapterView or ListView and implement the layout of the elements yourself. As you're getting each item from the Adapter, you'll measure and layout it, depending on the sizes of the surrounding adapter items.

A different solution that could work for you if you don't have a large number of elements is to use two custom Adapters, one called ItemAdapter and one called RowAdapter. The ItemAdapter will inflate the actual items based on (the presumed) Cursor. The RowAdapter will use the ItemAdapter to get the items and merge them into rows. The ListView will in turn use the RowAdapter. The issue is that to know how many rows the RowAdapter will produce, it is necessary to measure all the items from ItemAdapter before the RowAdapter is connected to the ListView.

彩虹直至黑白 2024-12-27 15:17:03

据我所知,目前还没有现成的解决方案可以解决您的问题。我还没有尝试过这样的事情,但我会使用 LinearLayout 作为列表项。然后只需创建您自己的 Deflater(例如 CursorAdapter)来缩小布局并检查长度。如果需要,您应该能够向 LinearLayout 添加新视图(例如 TextView)。

As far as I know there is no ready solution for your problem. I haven't yet tried something like this, but I would use a LinearLayout as the list item. Then just create your own Deflater (e.g. CursorAdapter) that deflates the Layout and checks the length. If needed you should be able to add a new View (e.g. TextView) to the LinearLayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文