调用
我制作了一个自定义LayoutManager,用于在单击左键或右键后平滑滚动。仅向左滚动时一切正常!对,computeScrollVectorForPosition
从不调用事件。什么东西?我试过在正确的时候设置mReverseLayout
,但这没有帮助。有什么我没有做/忽略的吗?
public class SmoothScrollLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 50f;
private Context context;
public boolean shouldGoRight = false;
public SmoothScrollLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
this.context = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
if (getChildCount() == 0) {
return null;
}
final int firstChildPos = getPosition(getChildAt(0));
final int direction = targetPosition < firstChildPos != shouldGoRight ? -1 : 1;
return new PointF(direction, 0);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
}
comupteScrollVectorForPosition
只是为了找出LinearSmoothScroller
必须滚动才能最终找到元素的方向。如果LinearSmoothScroller已经想知道元素在哪里,它将不会调用此函数。对于要显示的已经加载的元素,例如右侧的元素,就是这种情况。