1.在recyclerView中,scrollToPosition 不会触发 scrollListener,而 smoothScrollToPosition 会触发 scrollListener 。源码分析可参考 2.监听 smoothScrollToPosition 滑动动画停止时,的例子:
private void scrollToPosition(int position){
recyclerView.removeOnScrollListener(onScrollListener);
recyclerView.addOnScrollListener(onScrollListener);
recyclerView.smoothScrollToPosition(position);
}
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
switch (newState) {
case SCROLL_STATE_IDLE:
recyclerView.removeOnScrollListener(this);
break;
}
}
};
其中 SCROLL_STATE_IDLE 状态会在滑动动画结束时,触发。更多状态可参考
|