section indexer get section for position returns 0

前端 未结 2 2364
难免孤独
难免孤独 2020-12-11 12:48

In my project I have class that extends ArrayAdapter and implements SectionIndexer. When implementing methods getPositionForSec

2条回答
  •  抹茶落季
    2020-12-11 13:39

    Your callback method getPositionForSection(int section) needs to be implemented to give the right position which is used by setSelection to return the right position where you want to scroll on touch of SectionIndexer on rightside.

    Make sure you are doing this

    this.setSelection(((SectionIndexer) getAdapter()) .getPositionForSection(currentPosition));
    

    You need to implement two callback methods

    @Override
    public Object[] getSections() { 
      Log.d("ListView", "Get sections"); 
      String[] sectionsArr = new String[sections.length()]; 
      for (int i=0; i < sections.length(); i++) 
      sectionsArr[i] = "" + sections.charAt(i);
    
       return sectionsArr; 
     }
    

    This final callback and you are done here

    @Override 
    public int getPositionForSection(int section){ 
      Log.d("ListView", "Get position for section"); 
      for (int i=0; i < this.getCount(); i++) { 
      String item = this.getItem(i).toLowerCase(); 
      if (item.charAt(0) == sections.charAt(section)) 
    
     return i; 
    } 
    
    return 0; 
    }
    

提交回复
热议问题