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