android music中索引listview实现

本文介绍了一种自定义的SectionIndexer实现方法,通过继承AlphabetIndexer并重写compare方法来支持快速滚动功能。该方法适用于Android平台的ListView组件,能够帮助用户更快地定位到指定字母开头的内容部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private class MyAdapter extends SimpleCursorAdapter implements SectionIndexer {

  private MusicAlphaIndexer indexer;
  private String index = "ABCDEFGHIJKLMNOPQRSTUVWXYZ*";
  Map<Integer, Integer> pos = new HashMap<Integer, Integer>();
  
  public MyAdapter(Context context, int layout, Cursor c, String[] from,
    int[] to) {
   super(context, layout, c, from, to);
   indexer = new MusicAlphaIndexer(c, 1, index);
  }

  @Override
  public int getPositionForSection(int section) {
   return indexer.getPositionForSection(section);
  }

  @Override
  public int getSectionForPosition(int position) {
   Log.d("tag", "getSectionForPosition " + indexer.getSectionForPosition(position));
//   return indexer.getSectionForPosition(position);
   return 0;
  }

  @Override
  public Object[] getSections() {
   return indexer.getSections();
  }
 }
 
 private class MusicAlphaIndexer extends AlphabetIndexer {

  public MusicAlphaIndexer(Cursor cursor, int sortedColumnIndex,
    CharSequence alphabet) {
   super(cursor, sortedColumnIndex, alphabet);
  }

  @Override
  protected int compare(String word, String letter) {
   String key1 = Audio.keyFor(word);
   String key2 = Audio.keyFor(letter);
   if(key2.startsWith(key1)) {
    return 0;
   }
   return key1.compareTo(key2);
  }
 }

 

1。必须要重写AlphabetIndexer中的compare

2。Audio.KeyFor();是为根据一定的规则,生成一个用户不可读的串,进行比较

3。最后没有被匹配的数据,都属于最后一个,这里是"*"

4。要设置listview.setFastScrollEnabled(true);

5。数据不够,不会显示快速的滑块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值