滚动到列表中的字母(ArrayCollection dataProvider)(字母跳转)

时间:2011-10-04 03:53:43

标签: actionscript-3 flex actionscript air

希望这很容易,但有时候这意味着它在弹性方面是不可能的,而且我已经搜索了很多而无济于事。

说我有一个艺术家名单(列表#1):

2Pac的 阿黛尔 艾蜜蕊 碧昂丝 杰森阿尔丁 夏奇拉 特鲁斯

我还有一个列表(列表#2),其值为#,A-Z - 我将如何创建字母跳转?

因此,如果用户点击了LIST#2中的“A”,它会自动滚动到列表#1顶部的“Adele” - 不会过滤,所以他/她可以向上滚动查看2Pac或向下查看The Tews如果他们还不在视野中。

它是一个带有ArrayCollection作为dataProvider的标准Flex Spark List - 艺术家字段被称为:“title”以及用户不可见的唯一id字段。

谢谢!

请参阅有关字典讨论的标记答案的评论,在某些情况下可能会更快。请参阅下面的代码(尚未确认其更快!请测试):

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();
        }

2 个答案:

答案 0 :(得分:1)

Flex Spark列表有一个非常方便的方法ensureIndexIsVisible(index)。检查Flex reference documentation。您所要做的就是找到相应选定字母的第一位艺术家的索引:

public function findAlphabetJumpIndex():Number
{
    var jumpToIndex:Number;
    var selectedLetter:String = alphabethList.selectedItem;
    for (var i:int=0; i < artists.length; i++)
    {
        var artistName:String = artists.getItemAt(i);
        var artistFirstLetter:String = artistName.toUpperCase().charAt(0);
        if (artistFirstLetter == selectedLetter)
        {
            jumpToIndex = i;
            break;
        }
    }
    return jumpToIndex;
}

答案 1 :(得分:0)

您可以迭代您的艺术家列表数据提供者,并检查艺术家姓名是否从列表二中选定的字母开头。找到相应的艺术家后,设置艺术家列表选择索引的值是从迭代数据中获得的值。