我正在尝试使平滑滚动条需要在 recyclerView 中的位置。我是 LinearLayoutManager 中的 ovverride 方法 smoothScrollToPosition,它工作正常。但是,我需要在屏幕顶部设置滚动位置(如果可能的话)。我也试过scrollToPositionWithOffset,它是我需要的,但现在没有平滑效果。如何混合使用这些方法,并在顶部设置项目进行平滑滚动?
private LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this){
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
RecyclerView.State state, final int position) {
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(getApplicationContext()) {
//This controls the direction in which smoothScroll looks
//for your view
@Override
public PointF computeScrollVectorForPosition
(int targetPosition) {
return this
.computeScrollVectorForPosition(targetPosition);
}
//This returns the milliseconds it takes to
//scroll one pixel.
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return 50f/displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
};