1
我做了一個自定義佈局管理器來平滑滾動後點擊左鍵或右鍵。一切只在滾動時才起作用!對於,computeScrollVectorForPosition
永遠不會被調用。是什麼賦予了?我試過設置mReverseLayout
時,但沒有幫助。任何我沒有做的事/俯視?Android - 水平滾動左右使用RecyclerView
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);
}
}