2011-11-19 103 views
41

我的目的是爲了獲得類似的東西:
http://appsreviews.com/wp-content/uploads/2010/08/Cures-A-Z-App-for-iPhone.jpg如何創建一個按字母順序排列的滾動條,顯示android中的所有字母?

但唯一的例子我能看到的是列出這樣的:
android - listview fastscroll with alphabet like on iPhone contacts activity

很顯然,我不希望像聯繫人的列表,其在快速滾動時顯示字母。我知道如何做到這一點。

任何指針都會受到歡迎。 (我試過this但沒有成功)

下面,完整的解決方案通過FunkTheMonk的建議(非常感謝):

定義列表視圖如常。定義一個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); 
} 
+0

這裏http://stackoverflow.com/questions/7994959/how-to-add-section-header-to-custom-list-view-in-android檢查我的答案/ 8047328#8047328 –

+0

嗨,感謝您的回答,但是這顯示瞭如何執行標題,這不是我所需要的...檢查圖像。我想要的是右側的字母列表,當我點擊一個字母時,列表將滾動以此字母開頭的第一個元素。 – Sephy

+0

@Sephy我沒有收到onClick活動,請你解釋一下它是如何工作的? –

回答

39

這是一個iPhone的功能,Android使用快速滾動。我建議您使用平臺替代方案,而不是嘗試強制執行通用功能。

如果你必須,你必須自己實現這個。將你的列表視圖放在一個RelativeLayout中,並將A-Z TextView放置在一個垂直LinearLayout中,該垂直LinearLayout設置爲layout_alignParentRight="true"。將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); 
} 

這將滾動到所選字母的onClick,但我相信你可以在iPhone上的字母滾動你的手指,它會更新列表?你必須實現一個onTouchListener而不是onClickListener。

+2

是的,我知道這是一個iphone功能,但它不適合我。所以我正在做我所要求的。儘管我同意你的觀點,但遵循android的方式會更好。任何關於如何獲得分隔符的指針? – Sephy

+0

太棒了,它完美的作品!非常感謝 ! – Sephy

+1

謝謝。它工作正常。還要向TextViews添加android:clickable =「true」。 –

24

您可以嘗試使用IndexScroller,這些只有在觸摸時纔可見,並且在沒有接觸時會自動隱藏。 下面是截圖

screenshot

+1

這正是我需要的!謝謝。 –

+0

這是不是爲我工作 –

+0

@PankajNimgade:創建新問題並闡述什麼你嘗試和它是怎麼不行:) –