2015-11-03 132 views
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); 
} 

}

回答

0

comupteScrollVectorForPosition只打电话找出来到其中的LinearSmoothScroller有滚动到最终找到该元素的方向。如果LinearSmoothScroller已经想知道元素在哪里,它将不会调用这个函数。已经加载显示的元素就是这种情况,就像右侧的元素一样。