希望这很容易,但有时这意味着它在flex中不可能,我已经搜索了一些无济于事。滚动到列表中的字母表(ArrayCollection dataProvider)(字母跳转)
说我有一个表(表#1)的艺术家:
2Pac的 阿黛尔 艾蜜蕊 碧昂丝 杰森·奥尔德恩 夏奇拉 的Trews
我也有一个表(表#2)具有值#,AZ - 我将如何创建字母跳转?
因此,如果用户点击LIST#2中的“A”,它将自动滚动到LIST#1顶部的“Adele” - 不过滤,因此他/她可以向上滚动查看2Pac或向下查看提示他们是否还没有看到。
它是一个标准的Flex Spark List,带有一个ArrayCollection作为dataProvider - 艺术家字段被称为:“title”以及用户不可见的唯一id字段。
谢谢!
请查看关于标记答案的评论,以便在某些情况下可能会更快速地查找字典。请参见下面的代码(未证实ITS更快请测试!):
private function alphabet_listChange(evt:IndexChangeEvent) : void {
var letter:String;
letter = evt.currentTarget.selectedItems[0].toString();
trace(currentDictionary[letter]);
ui_lstLibraryList.ensureIndexIsVisible(currentDictionary[letter]);
}
public function createAlphabetJumpDictionary() : Dictionary {
//alphabetArray is a class level array containing, A-Z;
//alphabetDictionary is a class level dictionary that indexes A-z so alphabetDictionary["A"] = 0 and ["X"] = 25
var currentIndexDict:Dictionary = new Dictionary; //Dictionary is like an array - just indexed for quick searches - limited to key & element
var searchArray:Array = new Array;
searchArray = currentArrayCollection.source; //currentArrayCollection is the main array of objects that contains the titles.
var currentIndex:Number; //Current index of interation
var currentAlphabetIndex:Number = 0; //Current index of alphabet
for (currentIndex = 0; currentIndex < searchArray.length; currentIndex++) {
var titleFirstLetter:String = searchArray[currentIndex].title.toString().toUpperCase().charAt(0);
if (titleFirstLetter == alphabetArray[currentAlphabetIndex]) {
currentIndexDict[titleFirstLetter] = currentIndex;
trace(titleFirstLetter + " - " + currentIndex);
currentAlphabetIndex++;
} else if (alphabetDictionary[titleFirstLetter] > alphabetDictionary[alphabetArray[currentAlphabetIndex]]) {
trace(titleFirstLetter + " - " + currentIndex);
currentIndexDict[titleFirstLetter] = currentIndex;
currentAlphabetIndex = Number(alphabetDictionary[titleFirstLetter] + 1);
}
}
return currentIndexDict;
}
private function build_alphabeticalArray() : Array {
var alphabetList:String;
alphabetList = "A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z";
alphabetArray = new Array;
alphabetArray = alphabetList.split(".");
return alphabetArray;
}
private function build_alphabetDictionary() : Dictionary {
var tmpAlphabetDictionary:Dictionary = new Dictionary;
for (var i:int=0; i < alphabetArray.length; i++) {
tmpAlphabetDictionary[alphabetArray[i]] = i;
trace(alphabetArray[i] + " - " + i);
}
return tmpAlphabetDictionary;
}
private function buildCurrentDictionary() : void {
trace("Collection Changed");
currentDictionary = new Dictionary;
currentDictionary = createAlphabetJumpDictionary();
}
“你所要做的就是找到相应的选定字母的第一位艺术家的索引” - 这是我遇到困难的部分。这将如何呢? –
谢谢弗拉基米尔,我有同样的想法,现在正在使用它。我希望有一种不同的方法来解决这个问题 - 一个没有使用循环的长列表可能会导致一个问题 - 这是在移动设备上不幸的。 –
更快的方法是在加载艺术家时创建一个字典,然后通过字典检索艺术家索引。 –