场景: 比如排行榜, 我的位置,放到列表中间去。
平常若是只为了设置选中,只需要setSelection(position)就可以选中,但是位置一般是在列表的第一个
<span style="white-space:pre"> </span>int index = calcMyIndex;
if(listCount > mListVisibleCount){
if(listCount - calcMyIndex > mListVisibleCount / 2 + 1 ){
index = calcMyIndex - mListVisibleCount /2;
}
}
LOG.d("selected index = > "+index);
mRankingListView.setSelection(calcMyIndex);
mRankingListView.smoothScrollToPositionFromTop(index, - mItemViewHeight/2,0);
上面是代码, 一个是我的位置,另外一个index是计算后的第一个位置。
也就是当我的位置在中间,那么第一个应该就是我的位置减去 可见数量的一半
smoothScrollToPositionFromTop 中第二个参数
int offset,
我这用的是,itemview高度一般, 负值就是listview往上移一点。
要不然不带offset,第一个item是显示完整的~