Recyclerview滑动对齐方式smoothScrollToPosition

Recyclerview滑动对齐方式

原因

遇到一个问题,就是常见的双击让Recyclerview跳到知道position,但是跳转却是到对应position的底部,而不是上面对齐。
方法:smoothScrollToPosition(position)
Recyclerview空实现smoothScrollToPosition,最后由对应的LayoutManager实现,这里是LinearLayoutManager

 @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
            int position) {
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext());
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }

实现在LinearSmoothScroller:然后我发现了解决问题的位置了:

  /**
     * Align child view's left or top with parent view's left or top
     *
     * @see #calculateDtToFit(int, int, int, int, int)
     * @see #calculateDxToMakeVisible(android.view.View, int)
     * @see #calculateDyToMakeVisible(android.view.View, int)
     */
    public static final int SNAP_TO_START = -1;

    /**
     * Align child view's right or bottom with parent view's right or bottom
     *
     * @see #calculateDtToFit(int, int, int, int, int)
     * @see #calculateDxToMakeVisible(android.view.View, int)
     * @see #calculateDyToMakeVisible(android.view.View, int)
     */
    public static final int SNAP_TO_END = 1;

    /**
     * <p>Decides if the child should be snapped from start or end, depending on where it
     * currently is in relation to its parent.</p>
     * <p>For instance, if the view is virtually on the left of RecyclerView, using
     * {@code SNAP_TO_ANY} is the same as using {@code SNAP_TO_START}</p>
     *
     * @see #calculateDtToFit(int, int, int, int, int)
     * @see #calculateDxToMakeVisible(android.view.View, int)
     * @see #calculateDyToMakeVisible(android.view.View, int)
     */
    public static final int SNAP_TO_ANY = 0;

SNAP_TO_START 将子视图的左侧或顶部与父视图的左侧或顶部对齐
SNAP_TO_END 将子视图的右侧或底部与父视图的右侧或底部对齐
SNAP_TO_ANY 确定是否应该从头开始或从头开始捕捉孩子,具体取决于它当前相对于其父对象的位置

由于一直返回的是SNAP_TO_END,所以才导致是底部对齐了。那那里会用上这个了:

 /**
     * When scrolling towards a child view, this method defines whether we should align the left
     * or the right edge of the child with the parent RecyclerView.
     *滚动到子视图时,此方法定义我们是否应将子项的左*或右边缘与父RecyclerView对齐。
     * @return SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector
     * @see #SNAP_TO_START
     * @see #SNAP_TO_END
     * @see #SNAP_TO_ANY
     */
    protected int getHorizontalSnapPreference() {
        return mTargetVector == null || mTargetVector.x == 0 ? SNAP_TO_ANY :
                mTargetVector.x > 0 ? SNAP_TO_END : SNAP_TO_START;
    }

    /**
     * When scrolling towards a child view, this method defines whether we should align the top
     * or the bottom edge of the child with the parent RecyclerView.
     *滚动到子视图时,此方法定义我们是否应将子项的顶部*或底部边缘与父级RecyclerView对齐
     * @return SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector
     * @see #SNAP_TO_START
     * @see #SNAP_TO_END
     * @see #SNAP_TO_ANY
     */
    protected int getVerticalSnapPreference() {
        return mTargetVector == null || mTargetVector.y == 0 ? SNAP_TO_ANY :
                mTargetVector.y > 0 ? SNAP_TO_END : SNAP_TO_START;
    }

所以可以根据这两个方法的返回固定的值,来确定对齐方式。

方案

继承LinearSmoothScroller,重新上面2个方法,返回固定的对齐方式。

/**
 * 滚动到指定项然后和父布局顶部对齐
 */
public class TopSmoothScroller extends LinearSmoothScroller {

    TopSmoothScroller(Context context) {
        super(context);
    }

    @Override
    protected int getHorizontalSnapPreference() {
        return SNAP_TO_START;
    }

    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;  // 将子view与父view顶部对齐
    }
}

使用就直接调用,或者重新继承LinearLayoutManager的smoothScrollToPosition里面创建TopSmoothScroller。
这里直接调用了:

TopSmoothScroller smoothScroller = new TopSmoothScroller(mRecyclerView.getContext());
            smoothScroller.setTargetPosition(position);
            mRecyclerView.getLayoutManager().startSmoothScroll(smoothScroller);

控制速度了?

控制速度也是在LinearSmoothScroller类里面,只需要重新calculateSpeedPerPixel方法就行了:

public class TopSmoothScroller extends LinearSmoothScroller {

    TopSmoothScroller(Context context) {
        super(context);
    }

    @Override
    protected int getHorizontalSnapPreference() {
        return SNAP_TO_START;
    }

    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;  // 将子view与父view顶部对齐
    }

    @Nullable
    @Override
    public PointF computeScrollVectorForPosition(int targetPosition) {
        return super.computeScrollVectorForPosition(targetPosition);
    }

    @Override
    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
        //返回滑动一个pixel需要多少毫秒
        return 30/displayMetrics.density;
    }

   
}

注意的是,这里就必须继承LinearLayoutManager来使用了:

public class ScrollSpeedLinearLayoutManger extends LinearLayoutManager {
    private float MILLISECONDS_PER_INCH = 0.03f;
    private Context contxt;

    public ScrollSpeedLinearLayoutManger(Context context) {
        super(context);
        this.contxt = context;
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        TopSmoothScroller topSmoothScroller = new TopSmoothScroller(contxt);
        topSmoothScroller.setTargetPosition(position);
        startSmoothScroll(topSmoothScroller);
    }
    
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值