37SectionIndexer的使用

来源:互联网 发布:orcl 删除重复数据 编辑:程序博客网 时间:2024/05/29 19:56

在做通讯录联系人列表的时候,需要有字幕索引,选择SectionIndexer这个接口,在Adapter中实现这个接口。API中对这个接口的说明如下:

Interface that may implemented on Adapters to enable fast scrolling between sections of anAbsListView.

A section is a group of list items that have something in common. For example, they may begin with the same letter or they may be songs from the same artist. 

Adapter可能实现的接口,用来在AbsListView(他是ListView的直接父类)部分中快速滑动。section是一组ListView的条目的组合,它们有共同的地方,比如,它们可能以相同的字母开头,也可能是一些歌,这些歌的作者是一个人。

该接口的源码如下:

/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.widget;/** * Interface that may implemented on {@link Adapter}s to enable fast scrolling * between sections of an {@link AbsListView}. * <p> * A section is a group of list items that have something in common. For * example, they may begin with the same letter or they may be songs from the * same artist. * <p> * {@link ExpandableListAdapter}s that consider groups and sections as * synonymous should account for collapsed groups and return an appropriate * section/position. * * @see AbsListView#setFastScrollEnabled(boolean) */public interface SectionIndexer {    /**     * Returns an array of objects representing sections of the list. The     * returned array and its contents should be non-null.     * <p>     * The list view will call toString() on the objects to get the preview text     * to display while scrolling. For example, an adapter may return an array     * of Strings representing letters of the alphabet. Or, it may return an     * array of objects whose toString() methods return their section titles.     *     * @return the array of section objects     */    Object[] getSections();    /**     * Given the index of a section within the array of section objects, returns     * the starting position of that section within the adapter.     * <p>     * If the section's starting position is outside of the adapter bounds, the     * position must be clipped to fall within the size of the adapter.     *     * @param sectionIndex the index of the section within the array of section     *            objects     * @return the starting position of that section within the adapter,     *         constrained to fall within the adapter bounds     */    int getPositionForSection(int sectionIndex);    /**     * Given a position within the adapter, returns the index of the     * corresponding section within the array of section objects.     * <p>     * If the section index is outside of the section array bounds, the index     * must be clipped to fall within the size of the section array.     * <p>     * For example, consider an indexer where the section at array index 0     * starts at adapter position 100. Calling this method with position 10,     * which is before the first section, must return index 0.     *     * @param position the position within the adapter for which to return the     *            corresponding section index     * @return the index of the corresponding section within the array of     *         section objects, constrained to fall within the array bounds     */    int getSectionForPosition(int position);}

该接口中有两个重要的方法:

其一:

int getPositionForSection(int sectionIndex);

其二:

int getSectionForPosition(int position);

getSectionForPosition()通过该项的位置,获得所在分类组的索引号

getPositionForSection()       根据分类列的索引号获得该序列的首个位置

(来自:http://blog.csdn.net/jack_l1/article/details/14165291)



getSectionForPosition(0) 返回 0

getSectionForPosition(1) 返回 0

getSectionForPosition(2) 返回 0

getSectionForPosition(3) 返回 1

这下,明白这两个方法的区别了把

所以一般有如下代码,来确定是否滑动到了分类的首字母位置

  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,                      int totalItemCount) {                  int section = indexer.getSectionForPosition(firstVisibleItem);  //获得索引号                int nextSecPosition = indexer.getPositionForSection(section + 1);  //根据索引号获得下一个索引的初始位置                if (firstVisibleItem != lastFirstVisibleItem) {                      MarginLayoutParams params = (MarginLayoutParams) titleLayout.getLayoutParams();                      params.topMargin = 0;                      titleLayout.setLayoutParams(params);                      title.setText(String.valueOf(alphabet.charAt(section)));                  }                  if (nextSecPosition == firstVisibleItem + 1) {  //若是下一个索引号的位置与当前所见的项+1,则进行位移                    View childView = view.getChildAt(0);                      if (childView != null) {                          int titleHeight = titleLayout.getHeight();                          int bottom = childView.getBottom();                          MarginLayoutParams params = (MarginLayoutParams) titleLayout                                  .getLayoutParams();                          if (bottom < titleHeight) {                              float pushedDistance = bottom - titleHeight;                              params.topMargin = (int) pushedDistance;                              titleLayout.setLayoutParams(params);                          } else {                              if (params.topMargin != 0) {                                  params.topMargin = 0;                                  titleLayout.setLayoutParams(params);                              }                          }                      }                  }                  lastFirstVisibleItem = firstVisibleItem;              }          });  





0 0
原创粉丝点击