使用smoothScrollToPosition水平滚动recycleview不起作用

时间:2016-07-14 05:05:22

标签: android

我遇到滚动回收问题。 这是我的LinearlayoutManager:

public class LayoutManagerCustom  extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 50f;
private Context mContext;

public LayoutManagerCustom(Context context) {
    super(context);
    mContext = context;
}

public LayoutManagerCustom(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
    this.mContext = context;
}



@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
                                   final RecyclerView.State state, final int position) {

    LinearSmoothScroller smoothScroller =
            new LinearSmoothScroller(mContext) {

                //This controls the direction in which smoothScroll looks
                //for your view
                @Override
                public PointF computeScrollVectorForPosition
                (int targetPosition) {
                    return LayoutManagerCustom.this
                            .computeScrollVectorForPosition(targetPosition);
                }

                //This returns the milliseconds it takes to
                //scroll one pixel.
                @Override
                protected float calculateSpeedPerPixel
                (DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH/displayMetrics.densityDpi;
                }
            };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}


@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    return super.scrollHorizontallyBy(dx, recycler, state);

}

}

这是我创建RecycleView的代码:

 layoutManager = new LayoutManagerCustom(getContext(), LinearLayoutManager.HORIZONTAL,false);
    recyclerViewItem.setLayoutManager(layoutManager);

    recyclerViewItem.setAdapter(recycleViewItemAdapter);
    params.smoothScrollToPosition(recyclerViewItem, new RecyclerView.State(),Constant.NUM_DATE/2);

使用调试模式,我看到了

  

computeScrollVectorForPosition

如果使用VERTICAL创建layoutManager但是不调用HORIZONTAL,则会调用

以下是关于此的示例: Customizing SmoothScroller for the RecyclerView

感谢阅读。

2 个答案:

答案 0 :(得分:0)

如果平滑滚动目标的视图距离初始位置足够近,则不会调用computeScrollVectorForPosition方法。

通过使用HORIZONTAL创建layoutManager来使目标视图靠近? 如果是这样,你可以像这样改变速度:

public class LayoutManagerCustom  extends LinearLayoutManager {

    private static final float MILLISECONDS_PER_INCH_FOR_DECELERATION = 500f;

    ...

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView,
                                       final RecyclerView.State state, final int position) {

        LinearSmoothScroller smoothScroller =
                new LinearSmoothScroller(mContext) {

                    @Override
                    protected int calculateTimeForDeceleration(int dx) {
                        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
                        float MILLISECONDS_PER_PX = MILLISECONDS_PER_INCH_FOR_DECELERATION / displayMetrics.densityDpi;
                        int time = (int) Math.ceil(Math.abs(dx) * MILLISECONDS_PER_PX);
                        return  (int) Math.ceil(time / .3356);
                    }

                    ...

                };
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
}

答案 1 :(得分:0)

只需为CustomLinearLayoutManager类重写两个方法。

public class LayoutManagerCustom  extends LinearLayoutManager {

//All your code above here...

//        For Vertical scroll
        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }

//        For Horizontal scroll
        @Override
        protected int getHorizontalSnapPreference() {
            return SNAP_TO_START;
        }
}