SectionIndexer中的getSectionForPosition()与getPositionForSection()解惑
2016-05-30 20:47
549 查看
大家在做字母索引的时候常常会用到SectionIndexer这个类,里面有2个重要的方法
1. getSectionForPosition()通过该项的位置,获得所在分类组的索引号
2. getPositionForSection() 根据分类列的索引号获得该序列的首个位置
![](http://img.blog.csdn.net/20131105141604953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmFja19sMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
getSectionForPosition(0) 返回 0
getSectionForPosition(1) 返回 0
getSectionForPosition(2) 返回 0
getSectionForPosition(3) 返回 1
这下,明白这两个方法的区别了把
所以一般有如下代码,来确定是否滑动到了分类的首字母位置
[java] view
plain copy
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;
}
});
1. getSectionForPosition()通过该项的位置,获得所在分类组的索引号
2. getPositionForSection() 根据分类列的索引号获得该序列的首个位置
getSectionForPosition(0) 返回 0
getSectionForPosition(1) 返回 0
getSectionForPosition(2) 返回 0
getSectionForPosition(3) 返回 1
这下,明白这两个方法的区别了把
所以一般有如下代码,来确定是否滑动到了分类的首字母位置
[java] view
plain copy
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;
}
});
相关文章推荐
- 文件操作1-php
- 文件操作-php
- Atom 检测php错误扩展linter-php
- PHP在Windows上执行CMD命令的一个坑
- php 基础语法
- PHP=>Stream(流)的概念与用法
- PHP将页面中点击数量高的链接进行高亮显示的方法
- PHP开源管理后台 OSAdmin
- thinkphp标签
- PHP是世界上最好的编程语言,有例子说明!
- VSFTP设置虚拟用户
- ThinkPHP(5)——自定义函数库
- thinkphp 打印下最后执行的sql语句
- ThinkPHP(4)——URL伪静态
- 【Servlet】Response的OutputStream与Writer输出数据乱码的问题
- ThinkPHP(3)——URL模式
- php_pdo详解
- MVC中去掉index.php(优雅的使用url)
- tpc简介
- PHP如何实现跨域