【发布时间】:2012-12-26 19:40:03
【问题描述】:
我正在创建一个应用程序,我必须在右侧垂直栏中显示带有字母部分和首字母的 listView,如屏幕截图所示。 所以请帮助我如何使用字母部分索引器创建listView,索引在右上角。
谢谢
【问题讨论】:
标签: android list android-layout
我正在创建一个应用程序,我必须在右侧垂直栏中显示带有字母部分和首字母的 listView,如屏幕截图所示。 所以请帮助我如何使用字母部分索引器创建listView,索引在右上角。
谢谢
【问题讨论】:
标签: android list android-layout
将来可能会帮助某人。您可以做两件事来实现这一目标。首先创建一个可展开的列表视图,它将显示项目的实时状态,其中包含 A 到 Z 列表的部分,将其作为示例 http://javapapers.com/android/android-expandable-listview。
下一步是向其中添加节索引器。下面的链接适用于 listview http://www.survivingwithandroid.com/2012/12/android-listview-sectionindexer-fastscroll.html。
这会将可展开列表视图的滚动位置设置为用户在 Section Indexer 中触摸的位置。
private static String sections = "abcdefghilmnopqrstuvz";
this.setSelection(((SectionIndexer) getAdapter()) .getPositionForSection(currentPosition));//position of group view item
现在在getPositionForSection回调方法里面返回header的位置
@Override public int getPositionForSection(int sectionIndex) {
// Get the total number of groups
for (int i = 0; i < this.getGroupCount(); i++) {
//Get Group Item like,For 0 Group Item A,For 1 Group Item B etc
String groupItem = (String) this.getGroup(i);
int childCount = 0;
//Start Matching for letter B on section indexer, get the count of child
//for letter A and same logic for other letters
if (groupItem.charAt(0) == sections.charAt(sectionIndex)){
int previousChildIndex = i - 1;
//Run a for loop to get previous childs
for (int j = previousChildIndex; j >= 0; j--) {
//If for letter B, previous group Item i.e.A contains 3 childs
//the sum is maintained
childCount = childCount + this.getChildrenCount(j);
}
//for Group Item B, i=0 and childCount is 3 and so on
return i + childCount;
}
}
}
return 0;
}
使用示例中解释的相同逻辑应该对你有用!!!
【讨论】:
您可以在 github 上搜索打开的库。
您可以使用的库之一:Daniel Nam 的 IndexableListView
https://github.com/woozzu/IndexableListView
您可以按原样使用它,通过查看源代码来了解它;)
祝你好运
【讨论】: