Android 多列列表视图
我正在为我的应用程序创建一个排行榜、高分列表。我想以 4 列显示数据:排名、名称、创建者、得分,并通过对象列表相应地填充它们。我想在列表中一次显示 5 个项目,总共 20 个项目,使列表可滚动。我找不到合适的方法来做到这一点。由于我的工作截止日期即将到来,如果我能获得有关“如何做”的快速指南,那就太好了。
一百万谢谢:)
i am creating a leaderboard, highscore list for my application. I want to display the data in 4 columns, Rank, Name, Creator, Score and populate them accordingly by a list of objects. I want to display 5 items at once on the list with a total of 20 items on the list making the list scrollable. I cannot find a suitable way to go this. It would be great if i can get a quick guide on 'how to' since the deadline of my work is approaching.
thanks a million :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过定义自定义 ListView 轻松完成此操作。
要定义自定义列表视图,只需定义一个具有 4 个水平文本视图的自定义行布局文件即可。现在在 listview 的自定义适配器内膨胀此布局文件,为此您需要重写 getView() 方法并膨胀该行布局文件。
更新:
只需检查此 教程 来定义自定义列表视图,但使确保您通过定义具有 4 个水平文本视图的自定义行布局文件来使用本教程。
这是 row_layout.xml 文件:
You can easily done it with defining Custom ListView.
For defining custom listview, just define a custom row layout file with 4 textview in horizontal manner. Now inflating this layout file inside the custom adapter of listview, for that you need to override getView() method and inflate that row layout file.
Update:
Just check this tutorial to define custom listview, but make sure you use this tutorial by defining a custom row layout file with 4 horizontal textview.
Here is the row_layout.xml file:
终于找到解决办法了。我和 Android 以及 Xamarin for Android 新手有同样的问题。需要明确的是 - 我正在使用 Xamarin Studio 和用 C# 编写的 Xamarin for Android。
尽管 SimpleCursorAdapter 看起来可以容纳多个字段,但当绑定到 SimpleListItem 等内容时,仅使用一个字段。
我第一次尝试这个:
但我得到的只是光标行中的最后一个字段。
我了解到需要一个自定义列表视图。以下是四个文件的代码文件:
我已包含所有这些内容,以提供尽可能接近现实生活的完整工作示例尽可能。
这是 MainActivity.cs,它设置内容视图,使用游标从 SQLite 数据库获取数据,并定义
}
这是 Main.axml 文件:
最后是 myList.xml 文件,它本质上是 ListView 行的定义:
这是数据库文件:
问题:
SimpleListItem 控件应该拥有多个字段或至多一个复选框控件和标签吗?
应该使用网格控件吗?
替代方案:
与其做所有这些恶作剧,使用 SQL 简单地连接所需的值不是更容易吗?特别是因为协调两个文本控件的位置可能过于复杂。
全面披露:
该代码是我在其他帖子和论坛中阅读的代码的混合体,并根据我自己的特定要求进行了定制。
Finally found a solution. I Had the same question as someone new to Android and Xamarin for Android. To be clear - I'm using Xamarin Studio, and Xamarin for Android written in C#.
Although it seems like the SimpleCursorAdapter can accommodate multiple fields, when binding to something like the SimpleListItem only one field gets used.
I first tried this:
But all I ever got was the last field in the cursor row.
I learned that a CUSTOM LISTVIEW was needed. Following are code files for four files:
I have included all of this to provide as much as possible a completely working sample that is as close to real life as possible.
Here is MainActivity.cs which sets a content view, uses a cursor to get data from a SQLite database, and defines a
}
Here's the Main.axml file:
And finally, the myList.xml file which is essentially the definition of the ListView row:
And here's the database file:
Questions:
SHOULD a SimpleListItem control have more than a single field or at most a checkbox control and label?
Should a grid control be used instead?
Alternatives:
Instead of doing all these shenanigans, wouldn't it be easier to simple concatenate the needed values using SQL? Especially since coordinating the positioning of two text controls might be too complicated.
Full Disclosure:
This code is an amalgam of code I read in other posts and forums, and customized to fit my own particular requirements.
列表视图中的每个项目都是从布局文件中扩充的。在布局文件中,您可以添加四个相互水平对齐的textview。有关更多信息,您需要关注 EfficientAdapter,它会导致自定义 ListView。
Each item in the listview is inflated from a layout file. In the layout file, you can add four textview horizontally align to each other. For more information you need to focus on EfficientAdapter which leads to custom ListView.