如何在安卓系统中创建一个按字母排序的滚动条?

49
我的目的是获得类似这样的东西:
http://appsreviews.com/wp-content/uploads/2010/08/Cures-A-Z-App-for-iPhone.jpg 但我能找到的唯一例子就像这样的列表:
android - listview fastscroll with alphabet like on iPhone contacts activity 显然,我不想要像通讯录一样在快速滚动时显示字母的列表。我知道如何做到这一点。
任何指针都欢迎。 (我尝试了这个,但没有成功)
以下是FunkTheMonk建议的完整解决方案:

定义常规的listview。定义一个RelativeLayout,其中包含ListView和右侧的LinearLayout与所有字母。为了更好的解决方案,字母列表可以动态生成,以仅在列表中显示字母。然后,在onClick方法中,添加滚动列表的行为:
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="28dip" />

    <LinearLayout android:orientation="vertical"
        android:layout_width="28dip" android:layout_height="wrap_content"
        android:layout_alignParentRight="true" android:background="@android:color/transparent" >

        <TextView android:id="@+id/A" android:text="A" android:tag="A"
            style="@style/alphabetTextView"/>
        <TextView android:id="@+id/B" android:text="B" android:tag="B"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/C" android:text="C" android:tag="C"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/D" android:text="D" android:tag="D"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/E" android:text="E" android:tag="E"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/F" android:text="F" android:tag="F"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/G" android:text="G" android:tag="G"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/H" android:text="H" android:tag="H"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/I" android:text="I" android:tag="I"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/J" android:text="J" android:tag="J"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/K" android:text="K" android:tag="K"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/L" android:text="L" android:tag="L"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/M" android:text="M" android:tag="M"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/N" android:text="N" android:tag="N"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/O" android:text="O" android:tag="O"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/P" android:text="P" android:tag="P"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/Q" android:text="Q" android:tag="Q"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/R" android:text="R" android:tag="R"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/S" android:text="S" android:tag="S"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/T" android:text="T" android:tag="T"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/U" android:text="U" android:tag="U"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/V" android:text="V" android:tag="V"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/W" android:text="W" android:tag="W"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/X" android:text="X" android:tag="X"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/Y" android:text="Y" android:tag="Y"
            style="@style/alphabetTextView" />
        <TextView android:id="@+id/Z" android:text="Z" android:tag="Z"
            style="@style/alphabetTextView" />

    </LinearLayout>
</RelativeLayout>

Java

@Override
public void onClick(View v) {
    String firstLetter = (String) v.getTag();
    int index = 0;
    if (stringList != null) {
        for (String string : stringList) {
            if (string.startsWith(firstLetter)) {
                index = stringList.indexOf(string);
                break;
            }
        }
    }
    lv.setSelectionFromTop(index, 0);
}

请查看我的答案,链接如下:https://dev59.com/rFzUa4cB1Zd3GeqP7Ny7#8047328 - Lalit Poptani
嗨,谢谢回答,但这展示了如何制作标题,这不是我需要的...请查看图片。我想要的是在右侧有一个字母列表,当我点击其中一个时,列表会滚动到以该字母开头的第一个元素。 - Sephy
@Sephy,我无法获取onClick事件,你能否解释一下它是如何工作的? - Pratik Dasa
请查看以下教程:https://dev59.com/G1kT5IYBdhLWcg3wf_pN#39295074 - sushildlh
2个回答

44

这是一个iPhone的特性,Android使用快速滚动。我建议您使用平台替代方案,而不是试图强制实现共同的功能。

如果必须要实现此功能,您将需要自己实现。将您的列表视图放在RelativeLayout中,并将A-Z TextViews放在一个垂直LinearLayout中,该LinearLayout设置为 layout_alignParentRight="true"。适当地设置TextView的标签为A-Z,并在所有TextView上设置 onClick="quickScroll"

在您的Activity中实现:

public void quickScroll(View v) {
    String alphabet = (String)v.getTag();
    //find the index of the separator row view
    list.setSelectionFromTop(index, 0);
}

这将在点击时滚动到所选字母,但我相信您可以在iPhone上用手指滑动字母表并更新列表?为此,您将不得不实现一个onTouchListener而不是onClickListener。


2
是的,我知道这是iPhone的功能,但这不适用于我。所以我正在按照被要求的方式去做。虽然我同意你的观点,遵循安卓的方法会更好。你有关于如何获得分隔符的任何指针吗? - Sephy
1
谢谢。它运行良好。还要在TextView中添加android:clickable="true"。 - Akshay Raut
谢谢您的解释,讲得很清楚明白 :) - Subhalaxmi
其他语言呢? - instanceMaster

25
你可以尝试使用IndexScroller,它只在触摸时可见,并在没有接触时自动隐藏。 以下是截图

screenshot


@PankajNimgade:创建新的问题并详细说明您尝试了什么以及为什么不起作用:) - Trung Nguyen
@juandg,你是如何在代码中实现这个的?我从来没有得到右侧的字母。请帮忙。 - Pankaj Nimgade
@Jul,我已经尝试了你在答案中提到的https://github.com/woozzu/IndexableListView,并且几乎应用了所有类来实现它,但是如果我滚动列表,列表会显示并崩溃。 - Pankaj Nimgade
@PankajNimgade:你可以创建一个问题并添加相关信息。如果没有你的代码和崩溃日志,我无法帮助你。 - Trung Nguyen
如何将此附加到我的ListView? - ManishNegi
@ManishNegi:请查看此示例 https://github.com/woozzu/IndexableListView/blob/master/src/com/woozzu/android/indexablelistview/IndexableListViewActivity.java - Trung Nguyen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接