大家在做字母索引的時(shí)候常常會(huì)用到SectionIndexer這個(gè)類(lèi),里面有2個(gè)重要的方法
1. ? getSectionForPosition()通過(guò)該項(xiàng)的位置,獲得所在分類(lèi)組的索引號(hào)
2.?getPositionForSection() ? ? ? 根據(jù)分類(lèi)列的索引號(hào)獲得該序列的首個(gè)位置
getSectionForPosition(0) 返回 0
getSectionForPosition(1) 返回 0
getSectionForPosition(2) 返回 0
getSectionForPosition(3) 返回 1
這下,明白這兩個(gè)方法的區(qū)別了把
所以一般有如下代碼,來(lái)確定是否滑動(dòng)到了分類(lèi)的首字母位置
[java] view plaincopy
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)?{????????????????????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;??????????????}??????????});???? ?
總結(jié)
以上是生活随笔為你收集整理的SectionIndexer中的getSectionForPosition()与getPositionForSection()解惑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。