2010-10-10 4 views
15

Existe-t-il un moyen de réindexer un sectionIndexer après l'ajout de nouveaux éléments à ListView?Ré-indexer/Actualiser un sectionIndexer

J'ai trouvé ce solution, mais la superposition est la position dans le coin supérieur gauche après l'actualisation de SectionIndexer.

Vous avez des idées?

+0

que je fais quelque chose de similaire ICI http://stackoverflow.com/questions/10224233/alphabetindexer-with-custom-adapter-managed-by-loadermanager – toobsco42

Répondre

4

Une fois que le FastScroller (son dans AbsListView classe qui ListView étend à partir) obtient vos sections en appelant SectionIndexer#getSections(), il ne les re-obtient sauf si vous activez/désactivez-le défilement rapide comme mentionné dans le lien que vous avez mentionné. Pour afficher la valeur à l'écran, FastScroller appelle la méthode toString de la section.

Une solution possible est d'avoir une coutume SectionIndexer qui ont les caractéristiques suivantes:.

  • Le tableau des sections est de longueur fixe (longueur maximale du nombre prévu de sections Par exemple, si les sections représentent anglais alphabet, il sera 26)
  • avoir un objet personnalisé pour représenter les sections, plutôt que d'utiliser des chaînes
  • Ecraser la méthode toString de votre objet section personnalisée pour afficher ce que vous voulez sur la base des « valeurs de la section » actuelles.
  • -

par exemple. Dans votre mesure SectionIndexer

private int mLastPosition; 

public int getPositionForSection(int sectionIndex) { 
    if (sectionIndex < 0) sectionIndex = 0; 
    // myCurrentSectionLength is the number of sections you want to have after 
    // re-indexing the items in your ListView 
    // NOTE: myCurrentSectionLength must be less than getSections().length 
    if (sectionIndex >= myCurrentSectionLength) sectionIndex = myCurrentSectionLength - 1; 
    int position = 0; 
    // --- your logic to find the position goes in here 
    // --- e.g. see the AlphabeticIndexer source in Android repo for an example 

    mLastPosition = position; 
    return mLastPosition; 
} 

public Object[] getSections() { 
    // Assume you only have at most 3 section for this example 
    return new MySection[]{new MySection(), new MySection(), new MySection()}; 
} 

// inner class within your CustomSectionIndexer 
public class MySection { 
    MySection() {} 

    public String toString() { 
     // Get the value to displayed based on mLastPosition and the list item within that position 
     return "some value"; 
    } 
} 
1

J'ai trouvé que la meilleure façon de le faire est d'appeler setContentView(R.layout.whatever) puis repeupler la ListView avec votre nouvel adaptateur/nouveaux éléments de données. Cela va redessiner le ListView avec vos nouveaux objets et la superposition FastScroll apparaîtra au bon endroit.

0

J'ai trouvé notifyDataSetInvalidated fonctionne bien, voici l'idée:

public class MyAdapter extends XXXAdapter implements SectionIndexer { 
    ... 
    public void updateDataAndIndex(List data, Map index) { 
     // update sections 
     // update date set 
     notifyDataSetInvalidated(); 
    } 
} 

mise à jour vos données établies et index (sections) en quelque sorte, puis notifyDataSetInvalidated, l'indice se rafraîchir.

0

Vous pouvez forcer la liste des sections Rafraîch ListView par listView.setAdapter(yourAdapter)

Questions connexes