【发布时间】:2017-09-29 12:44:58
【问题描述】:
我是安卓开发新手。 我一直在寻找类似于联系人列表的列表视图,即右侧带有字母索引面板的列表。
谢谢。
【问题讨论】:
标签: android
我是安卓开发新手。 我一直在寻找类似于联系人列表的列表视图,即右侧带有字母索引面板的列表。
谢谢。
【问题讨论】:
标签: android
在 android 中没有类似的东西你必须创建自定义视图。尝试从iphone-uitable-view-in-android 和sideindex-for-android. 我已经使用这两个链接中的代码来创建类似iphone 的列表,旁边有字母。
【讨论】:
sideindex-for-android 中的代码比它需要的要复杂。
我像该示例一样创建了 LinearLayout,并为字母添加了 TextView 实例。但后来我让它们可以点击了。
在我的 onClick() 中,我查看它是否是这些视图之一并从中获取文本。
当我加载我的列表时,我在 cursoradapter 中设置了一个字典。 setSelection() 方法从这个字典中获取偏移量。
public static final String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public Map<String, Integer> getAlphabetOffsets() {
HashMap<String, Integer> map = new HashMap<>();
// First initialize the dictionary with all of the letters of the alphabet.
//
for (int idx = 0; idx < alphabet.length(); idx++) {
String aLetter = alphabet.substring(idx, idx+1);
map.put(aLetter, -1);
}
int numFound = cursor.getCount();
cursor.moveToFirst();
// Now go through the products' first initials and, when an initial has not been
// found, set the offset for that letter.
//
for (int idx = 0; idx < numFound; idx++) {
String productName = cursor.getString(cursor.getColumnIndex(DB.PRODUCTS_NAME_COL));
String current;
if (productName == null || productName.equals("")) {
current = "0";
} else {
current = productName.substring(0, 1).toUpperCase();
}
// By the way, what do we do if a product name does not start with a letter or number?
//
// For now, we will ignore it. We are only putting 0-9 and A-Z into the side index.
//
if (map.containsKey(current) && map.get(current) < 0)
map.put(current, idx);
cursor.moveToNext();
}
map.put("0", 0);
int lastFound = 0;
/*
Now we deal with letters in the alphabet for which there are no products.
We go through the alphabet again. If we do not have an offset for a letter,
we use the offset for the previous letter.
For example, say that we do not have products that start with "B" or "D", we
might see:
{ "9" = 0, "A" = 1, "B" = -1, "C" = 5, "D" = -1, "E" = 10 }
After this runs, will we have:
{ "9" = 0, "A" = 1, "B" = 1, "C" = 5, "D" = 5, "E" = 10 }
This is so if we click on B, we see the list starting a "A" and see that
there are no "B" products.
*/
for (int idx = 0; idx < alphabet.length(); idx++) {
String current = alphabet.substring(idx, idx+1);
if ( map.get(current) < 0 ) {
map.put(current, lastFound);
} else {
lastFound = map.get(current);
}
System.out.println("alphabet \"" + current + "\" = " + map.get(current));
}
return map;
}
【讨论】:
在 android 中没有类似的东西(三星的联系人视图除外)
android 中默认使用listview 和原生联系人列表一样快速滚动
【讨论】: