我想在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);
}
};
答案 0 :(得分:4)
这是迟到的回复,但我想建议一个库SnappySmoothScroller。
此库可以使用特定的捕捉位置进行平滑滚动:SnapType.START
,SnapType.CENTER
或SnapType.END
像这样使用:
private LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
SnappySmoothScroller scroller = new SnappySmoothScroller.Builder()
.setPosition(position)
.setSnapType(SnapType.START)
.setScrollVectorDetector(new LinearLayoutScrollVectorDetector(this))
.build(recyclerView.getContext());
startSmoothScroll(scroller);
}
};