如何创build一个按字母顺序排列的滚动条,在android中显示所有的字母?

我的目的是获得这样的东西:
http://appsreviews.com/wp-content/uploads/2010/08/Cures-A-Z-App-for-iPhone.jpg

但是我能find的唯一例子就是这样的列表:
android – listview快速滚动字母喜欢在iPhone联系人活动

很明显,我不想要一个像快速滚动时显示字母的联系人列表。 我知道如何做到这一点。

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

下面是FunkTheMonk提出的完整解决scheme(非常感谢):

像往常一样定义列表视图。 定义一个包含ListView的RelativeLayout,右边是一个包含所有字母的LinearLayout 。 为了更好的解决scheme,可以dynamic生成字母列表,只显示列表中的字母。 然后在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); } 

这是一个iPhonefunction,Android使用快速滚动。 我build议您使用平台替代scheme,而不是尝试强制执行通用function。

如果你一定要,你必须自己实现这个。 把你的列表视图放在一个RelativeLayout中,并把AZ TextViews放在一个垂直的LinearLayout中,该垂直的LinearLayout被设置为layout_alignParentRight="true" 。 将TextView的标签设置为AZ,并在其上设置onClick="quickScroll"

在您的活动中实施:

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

这将滚动到选定的字母onClick,但我相信你可以滚动你的手指在iPhone上的字母表,它会更新列表? 你必须实现一个onTouchListener而不是onClickListener。

您可以尝试使用仅在触摸时可见的IndexScroller ,没有联系时使用自动隐藏。 这是截图

截图

Interesting Posts