我有两个箭头按钮,用于滚动左右3个项目(或分别滚动到列表的开头或结尾)。向左滚动 - 目标位置小于当前位置 - 仅起作用。向右滚动时,甚至不会调用smoothScrollToPosition()
。有没有人碰到这个?我是否必须以某种方式撤销列表?
我对LinearLayoutManager的扩展:
public class SmoothScrollLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 50f;
private Context context;
public SmoothScrollLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
this.context = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) {
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SmoothScrollLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
}
来自LinearLayoutManager的computeScrollVectorForPosition(int targetPosition)
public PointF computeScrollVectorForPosition(int targetPosition) {
if (getChildCount() == 0) {
return null;
}
final int firstChildPos = getPosition(getChildAt(0));
final int direction = targetPosition < firstChildPos != mShouldReverseLayout ? -1 : 1;
if (mOrientation == HORIZONTAL) {
return new PointF(direction, 0);
} else {
return new PointF(0, direction);
}
}
我通过调用recyclerView.smoothScrollToPosition(position);
非常感谢任何帮助。我大部分时间都被困在这上面。