Android:如何使用SectionIndexer
我正在尝试找到一种使用 SectionIndexer
而不是 AlphabetIndexer
的方法。我感兴趣的是在节标题上使用字符串数组的元素而不是字母表。我无法使用部分索引器找到任何示例代码。
以下是 AlphabetIndexer
的示例代码:
private AlphabetIndexer indexer;
indexer = new AlphabetIndexer(c, c.getColumnIndexOrThrow(
DbHelper.COUNTRIES_NAME),"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
是否可以将 stringArray
而不是“ABCDEFGHIJKLMNOPQRSTUVWXYZ”传递给 AlphabetIndexer
,这样我就可以代替“A”,“B”,...“Z”作为标题,标题上有“Book”,“Food”,...?
如果不是,最好的方法是什么?任何对使用 SectionIndexer
而不是 AlphabetIndexer
的示例代码的引用也会有所帮助。
感谢您的帮助。 泰杰
I am trying to find a way to use SectionIndexer
, instead of AlphabetIndexer
. What I am interested to do is to have elements of a string arrays on the section headers instead of alphabets. I have not been able to find any sample code using section indexer.
Here is a sample code for AlphabetIndexer
:
private AlphabetIndexer indexer;
indexer = new AlphabetIndexer(c, c.getColumnIndexOrThrow(
DbHelper.COUNTRIES_NAME),"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Is it possible to pass a stringArray
instead of "ABCDEFGHIJKLMNOPQRSTUVWXYZ" to the AlphabetIndexer
so I can for example instead of "A", "B", ..."Z" as header have "Book", "Food", ...on the headers?
If not what is the best way to do that? Any reference to a sample code that uses SectionIndexer
instead of AlphabetIndexer
would be helpful as well.
Thanks for the help.
TJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试编写一个自定义的 ArrayAdapter ,并在 getView(...) 方法中返回一个“节标题”视图,以获取标题应出现的位置。
您还必须覆盖
getViewTypeCount ()
以返回新类型视图的数量(在本例中为 2),并覆盖getItemViewType (intposition)
以返回新类型视图的类型。查看当前位置。此外,
onItemClickListener
应检查您单击的项目是否是节标题。这是我的自定义数组适配器:
这是 row_item_default.xml 文件:
这是 row_item_section_header.xml:
ModelItem 类是一个简单的标题、副标题和布尔值容器,用于判断它是否是节标题。
这不是编写此适配器的唯一方法,但我希望这会有所帮助。
You can try to write a custom
ArrayAdapter
and basically return a "section header" view in thegetView(...)
method for the positions where headers should appear.You'll also have to overwrite
getViewTypeCount ()
to return the number of new types of views (in this case 2) andgetItemViewType (int position)
to return the type of view for the current position.Also, the
onItemClickListener
should check to see if the item you clicked on is a section header.This is my custom array adapter:
This is the row_item_default.xml file:
and this is the row_item_section_header.xml:
The ModelItem class is a simple container for title, subtitle and a boolean to tell if it's a section header or not.
This is not the only way to write this adapter but I hope this helps.