2011-01-26 50 views
0

我有一個Android應用程序,顯示約500人的自定義聯繫人列表,它工作正常;但是,如果我正在尋找以字母「s」開頭的名稱,則需要永久向下滾動。如何將UI添加到大的android listview?

我該如何編程才能跳到S而不滾動整個列表?

下面是XML佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:background="@color/background" 
    android:orientation="vertical"> 
    <ListView android="@+id/android:thelist" android:layout_height="0dip" 
    android:layout_width="wrap_content" 
    android:scrollbars="vertical" 
    android:fadingEdge="vertical" 
    android:padding="2dp" 
    android:fastScrollEnabled="true"> 
    </ListView> 

<TextView android:text="@+id/TextView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/label" 
android:textSize="14dip" 
android:textStyle="bold" android:paddingLeft="10dip" android:paddingTop="3dip"></TextView> 
</LinearLayout> 

回答

0

使用以下:

  1. 使用android:fastScrollEnabled="true"

  2. 啓用快速滾動確保您ListAdapter實現接口android.widget.SectionIndexer

通常很容易實現SectionIndexer。代碼大綱如下:

class SI extends ... implements SectionIndexer 
{ 
    getSection() { 
     return new String[] { "A", "B", ... }; 
    } 

    getSectionForPosition(position) { 
     //Just an example 
     Object obj = getItem(position); 
     char c = obj.toString().charAt(0); 
     return (c - 'A'); 
    } 

    getPositionForSection(section) { 
     List<?> items = getAllItems(); 
     char c = (char) ('A' + section); 
     int position = getFirstItemStartingWith(items, c); 
     return position; 
    } 
} 

讓我知道它是否工作。

-Gaurav
www.incoleg.com