2011-02-25 38 views
14

tôi đang tìm cách để bắt chước các chức năng trong ứng dụng Nhạc mới nhất, cụ thể là ít con trỏ đẹp bật lên cho phép một để di chuyển siêu nhanh để các nghệ sĩ/album/theo dõi họ đang tìm kiếm:Tạo cuộn chữ cái dễ dàng trong ListView?

"Super Scrolling" Regular Scrolling

Có phương pháp nào để bật chức năng như thế này trong một ListView trong SDK Android không?

+1

hãy lưu ý lỗi này: http://code.google.com/p/android/issues/detail?id=9054 – tomash

+0

Cảm ơn vì điều đó. Lưu ý. –

+0

Tôi đang làm một cái gì đó tương tự Ở đây http://stackoverflow.com/questions/10224233/alphabetindexer-with-custom-adapter-managed-by-loadermanager – toobsco42

Trả lời

14

Làm bộ điều hợp danh sách của bạn triển khai SectionIndexer. Ứng dụng nhạc cũng sử dụng AlphabetIndexer để thực hiện một số thao tác nâng hạng nặng. Cũng sử dụng setFastScrollEnabled(true) trên số thực tế ListView để kích hoạt tính năng này.

Chỉnh sửa: Nếu bạn không sử dụng CursorAdapter, bạn sẽ không thể sử dụng AlphabetIndexer. Bạn có thể thử xem xét việc thực hiện here và thấy khó khăn như thế nào để thích ứng với nó để làm việc với một ArrayAdapter.

+0

Ok, ngọt ngào, nhưng làm cách nào để triển khai 'SectionIndexer'? Vì adapter danh sách của tôi là một 'ArrayAdapter', làm thế nào tôi có thể lấy một con trỏ cho nó để chuyển tới' AlphabetIndexer'? Tôi có dữ liệu khá đơn giản mà tôi chỉ cần nhóm theo thứ tự bảng chữ cái theo tên, do đó mong muốn sử dụng 'AlphabetIndexer'. –

+0

Tuyệt vời, cảm ơn, tôi sẽ xem xét việc triển khai giao diện 'SectionIndexer' trong bộ điều hợp của tôi và cung cấp các phần của tôi. –

+1

mã mẫu: vui lòng xem http: //www.anddev.org/tutusing_alphabetindexer_for_fastscrolling_listview-t10282.html – DroidBot

2

Để thu thập các thông tin chi tiết về các khoản mục trình bày trong một listview, bạn có thể làm điều này:

@Override 
     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 

      albumButton.setText(songs.get(firstVisibleItem).getAlbum()); 

     } 

Đó không phải là một chút đơn giản hơn? Ở đây, 'bài hát' là một danh sách các đối tượng Bài hát. Bạn thậm chí có thể nhận được mục hiển thị cuối cùng bằng cách thêm firstVisibleItem + visibleItemCount. Tôi thấy kỹ thuật này rất hữu ích. Vì vậy, sau đó bạn sẽ nhận được chữ cái đầu tiên của mỗi bài hát. Tôi cho rằng hộp màu xám có chứa chữ cái trong ứng dụng nhạc bạn đã đăng là một hộp thoại sắp xếp?

Dù sao, hy vọng điều này sẽ hữu ích. Tôi nhận ra tôi đã trễ, nhưng điều này là dành cho những người tương lai

4

Đây là lớp con của ArrayAdapter mà tôi đang sử dụng. Lưu ý rằng đường chuyền số objects tôi đã được sắp xếp theo thứ tự abc, với Collections.sort(objects).

class SectionIndexingArrayAdapter<T> extends ArrayAdapter<T> implements 
     SectionIndexer { 

    HashMap<String, Integer> sectionsMap = new HashMap<String, Integer>(); 
    ArrayList<String> sectionsList = new ArrayList<String>(); 

    ArrayList<Integer> sectionForPosition = new ArrayList<Integer>(); 
    ArrayList<Integer> positionForSection = new ArrayList<Integer>(); 

    public SectionIndexingArrayAdapter(Context context, int textViewResourceId, 
      List<T> objects) { 
     super(context, textViewResourceId, objects); 

     // Note that List<T> objects has already been sorted alphabetically 
     // e.g. with Collections.sort(objects) **before** being passed to 
     // this constructor. 

     // Figure out what the sections should be (one section per unique 
     // initial letter, to accommodate letters that might be missing, 
     // or characters like ,) 
     for (int i = 0; i < objects.size(); i++) { 
      String objectString = objects.get(i).toString(); 
      if (objectString.length() > 0) { 
       String firstLetter = objectString.substring(0, 1).toUpperCase(); 
       if (!sectionsMap.containsKey(firstLetter)) { 
        sectionsMap.put(firstLetter, sectionsMap.size()); 
        sectionsList.add(firstLetter); 
       } 
      } 
     } 

     // Calculate the section for each position in the list. 
     for (int i = 0; i < objects.size(); i++) { 
      String objectString = objects.get(i).toString(); 
      if (objectString.length() > 0) { 
       String firstLetter = objectString.substring(0, 1).toUpperCase(); 
       if (sectionsMap.containsKey(firstLetter)) { 
        sectionForPosition.add(sectionsMap.get(firstLetter)); 
       } else 
        sectionForPosition.add(0); 
      } else 
       sectionForPosition.add(0); 
     } 

     // Calculate the first position where each section begins. 
     for (int i = 0; i < sectionsMap.size(); i++) 
      positionForSection.add(0); 
     for (int i = 0; i < sectionsMap.size(); i++) { 
      for (int j = 0; j < objects.size(); j++) { 
       Integer section = sectionForPosition.get(j); 
       if (section == i) { 
        positionForSection.set(i, j); 
        break; 
       } 
      } 
     } 
    } 

    // The interface methods. 

    public int getPositionForSection(int section) { 
     return positionForSection.get(section); 
    } 

    public int getSectionForPosition(int position) { 
     return sectionForPosition.get(position); 
    } 

    public Object[] getSections() { 
     return sectionsList.toArray(); 
    } 
} 
+0

Phương pháp này hoạt động tuyệt vời !!! Vấn đề duy nhất tôi có là tôi có một danh sách rất dài ... và phải mất một thời gian dài để có được tất cả các tiêu đề ... vv ... Tôi đã thử chạy những cái đó cho các vòng trong AsyncTask ... nhưng nó chỉ đóng băng mọi thứ ... Gợi ý? – DeNitE

0

Tôi tìm thấy giải pháp tuyệt vời here ... Hoạt động tốt với danh sách lớn ... Tốt và nhanh ... không tải thời gian.

@Override 
public int getPositionForSection(int section) 
{ 
    log.info("Get position for section"); 

    for (int i = 0; i < this.getCount(); i++) 
    { 
     if (this.getItem(i).length() > 0) 
     { 
      String item = this.getItem(i).toUpperCase(); 
      if (item.charAt(0) == sections.charAt(section)) 
      { 
       return i; 
      } 
     } 
    } 
    return 0; 
} 

@Override 
public int getSectionForPosition(int arg0) 
{ 
    log.info("Get section"); 

    return 0; 
} 

@Override 
public Object[] getSections() 
{ 
    log.info("Get sections"); 

    String[] sectionsArr = new String[sections.length()]; 

    for (int i = 0; i < sections.length(); i++) 
    { 
     sectionsArr[i] = "" + sections.charAt(i); 
    } 
    return sectionsArr; 
} 
Các vấn đề liên quan