在谷歌问题追踪器页面的第四楼中提到:
https://issuetracker.google.com/issues/36952786
“之前给出的解决方法是,在开始滚动时监听SCROLL_STATE_IDLE,然后再次smoothScrollToPositionFromTop到同一个位置。”但这种方法不总是有效。
实际上,调用带有SCROLL_STATE_IDLE的onScrollStateChanged并不一定意味着滚动已经完成。因此,它仍然无法保证ListView每次都能滚动到正确的位置,特别是当列表项视图的高度不完全相同时。
经过研究,我发现了另一种完美、正确和合理的方法。众所周知,Listview提供了一个scrollListBy(int y)方法,它可以让我们立即将Listview向上滚动y像素。然后,借助计时器,我们可以自己平滑地正确地滚动列表。
我们需要做的第一件事是计算每个列表项视图的高度,包括屏幕外的视图。由于列表数据和子视图类型已知,因此可以计算每个列表项视图的高度。因此,给定一个平滑滚动到的目标位置,我们可以计算其在y方向上的滚动距离。此外,计算应在完成初始化ListView之后进行。
第二件事是将计时器和scrollListBy(int)方法结合起来。实际上,我们可以使用android.os.Handler的sendEmptyMessageDelayed()方法。因此,解决方案可以是:
public class ListViewSmoothScroller {
private final static int MSG_ACTION_SCROLL = 1;
private final static int MSG_ACTION_ADJUST = 2;
private ListView mListView = null;
protected int[] mItemAccumulateHeight = null;
protected int mTimeStep = 20;
protected int mHeaderViewHeight;
private int mPos;
private Method mTrackMotionScrollMethod = null;
protected int mScrollUnit = 0;
protected int mTotalMove = 0;
protected int mTargetScrollDis = 0;
private Handler mMainHandler = new Handler(Looper.getMainLooper()){
public void handleMessage(Message msg) {
int what = msg.what;
switch (what){
case MSG_ACTION_SCROLL: {
int scrollDis = mScrollUnit;
if(mTotalMove + mScrollUnit > mTargetScrollDis){
scrollDis = mTargetScrollDis - mTotalMove;
}
if(Build.VERSION.SDK_INT >= 19) {
mListView.scrollListBy(scrollDis);
}
else{
if(mTrackMotionScrollMethod != null){
try {
mTrackMotionScrollMethod.invoke(mListView, -scrollDis, -scrollDis);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
mTotalMove += scrollDis;
if(mTotalMove < mTargetScrollDis){
mMainHandler.sendEmptyMessageDelayed(MSG_ACTION_SCROLL, mTimeStep);
}else {
mMainHandler.sendEmptyMessageDelayed(MSG_ACTION_ADJUST, mTimeStep);
}
break;
}
case MSG_ACTION_ADJUST: {
mListView.setSelection(mPos);
break;
}
}
}
};
public ListViewSmoothScroller(Context context, ListView listView){
mListView = listView;
mScrollUnit = Tools.dip2px(context, 60);
mPos = -1;
try {
mTrackMotionScrollMethod = AbsListView.class.getDeclaredMethod("trackMotionScroll", int.class, int.class);
}catch (NoSuchMethodException ex){
ex.printStackTrace();
mTrackMotionScrollMethod = null;
}
if(mTrackMotionScrollMethod != null){
mTrackMotionScrollMethod.setAccessible(true);
}
}
public void smoothScrollToPosition(int pos){
if(mListView == null)
return;
if(mItemAccumulateHeight == null || pos >= mItemAccumulateHeight.length){
return ;
}
mPos = pos;
mTargetScrollDis = mItemAccumulateHeight[pos];
mMainHandler.sendEmptyMessage(MSG_ACTION_SCROLL);
}
public void doMeasureOnLayoutChange(){
if(mListView == null){
return;
}
int headerCount = mListView.getHeaderViewsCount();
if(mListView.getChildCount() < headerCount + 1){
return ;
}
mHeaderViewHeight = 0;
for(int i = 0; i < headerCount; i++){
mHeaderViewHeight += mListView.getChildAt(i).getHeight();
}
View firstListItemView = mListView.getChildAt(headerCount);
computeAccumulateHeight(firstListItemView);
}
protected void computeAccumulateHeight(View firstListItemView){
int len = listdata.size();
mItemAccumulateHeight = new int[len + 2];
mItemAccumulateHeight[0] = 0;
mItemAccumulateHeight[1] = mHeaderViewHeight;
int currentHeight = mHeaderViewHeight;
for(int i = 2; i < len + 2; i++){
currentHeight += getItemHeight(firstListItemView);
mItemAccumulateHeight[i] = currentHeight;
}
}
protected int getItemHeight(View firstListItemView){
}
}
在初始化我们的ListView之后,我们调用doMeasureOnLayoutChange()方法。之后,我们可以通过smoothScrollToPosition(int pos)方法滚动ListView。我们可以像这样调用doMeasureOnLayoutChange()方法:
mListAdapter.notifyDataSetChanged();
mListView.post(new Runnable() {
@Override
public void run() {
mListViewSmoothScroller.doMeasureOnLayoutChange();
}
});
最后,我们的ListView可以平滑地滚动到目标位置,更重要的是,正确无误。