【问题标题】:How to create an alphabetical scrollbar displaying all the letter in android?如何创建一个按字母顺序显示android中所有字母的滚动条?
【发布时间】:2012-01-01 19:44:51
【问题描述】:

我的目的是获得类似的东西:

但我能找到的唯一例子是这样的列表:
android - listview fastscroll with alphabet like on iPhone contacts activity

显然,我不想要一个像联系人那样在您快速滚动时显示字母的列表。我知道该怎么做。

欢迎任何指针。 (我试过this但没有成功)

下面是 FunkTheMonk 建议的完整解决方案(非常感谢):

像往常一样定义列表视图。定义一个包含ListView 的RelativeLayout,并在右侧定义一个带有所有字母的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);
}

【问题讨论】:

  • 在这里查看我的答案stackoverflow.com/questions/7994959/…
  • 嗨,感谢您的回答,但这显示了如何做标题,这不是我需要的...检查图像。我想要的是右侧的字母列表,当我单击一个时,列表会滚动以该字母开头的第一个元素。
  • @Sephy 我没有收到 onClick 事件,你能解释一下它是如何工作的吗?

标签: android listview alphabetical


【解决方案1】:

这是 iPhone 的一项功能,Android 使用快速滚动。我建议您使用平台替代方案,而不是尝试强制执行通用功能。

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

在您的活动中实施:

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。

【讨论】:

  • 是的,我知道这是 iphone 的一项功能,但它不适合我。所以我正在做我被要求做的事情。虽然我同意你的看法,但遵守 android 的方式会更好。关于如何获取分隔符的任何指针?
  • 谢谢。它工作正常。还将 android:clickable="true" 添加到 TextViews。
  • 谢谢 .. 纽约市的解释 :)
  • 其他语言呢?
【解决方案2】:

您可以尝试使用IndexScroller,它只在触摸时可见,在没有接触时自动不可见。 这是截图

【讨论】:

  • 这正是我所需要的!谢谢。
  • @PankajNimgade:创建新问题并详细说明您尝试了什么以及它如何不起作用:)
  • @juandg,你是如何在代码中实现的,我从来没有得到右侧的字母表。请帮忙
  • @Jul,我已经尝试了您回答中提到的 github.com/woozzu/IndexableListView 并且几乎应用了所有类来实现这个,但是如果我滚动列表显示和崩溃列表
  • @PankajNimgade:您可以创建问题并添加相关信息。如果没有您的代码和崩溃日志,我无法为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 2019-12-16
相关资源
最近更新 更多