1

I've got a recylerview with an offset of 25dp to the left hand side of it. Meaning an item can be seen from the left to show that there's something in the list, the farmost left item is snapped into place, as shown here in my awful drawing; https://i.stack.imgur.com/Y0iwA.jpg.

I want to be able to scroll to the position of number 1 in the list smoothly. I've acheived the behaviour none smoothly using;

layoutManager.scrollToPositionWithOffset(position, 25)

Where 25 is the offset. But this just kind of jumpers there. I've also tried;

smoothScrollToPosition(position) 

with no success.

My latest is using a SmoothScroller which works, but only correctly in one direction. If my new position is after the current position it works fine, but if it is before the current position it goes about halfway through the one before the new position (see item 0 in the drawing) and then snaps back to 1 which isn't very nice. I understand that it's probably something to do with my calculations in my SmoothScroller;

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) {
                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                }

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

                @Override
                public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
                    return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
                }
            };

            smoothScroller.setTargetPosition(itemPosition);
            layoutManager.startSmoothScroll(smoothScroller);
        }

My SnapHelper attached to the RecylerView is a cut down version of the answer to this post - How to enable a PagerSnapHelper to snap in both directions in a RecyclerView?. With an extra bit of padding on the method;

private int distanceToStart(View targetView, OrientationHelper helper, boolean fromEnd) {
    if (mIsRtlHorizontal && !fromEnd) {
        return distanceToEnd(targetView, helper, true);
    }

    return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding() **- 25**;
}

Where I've added the 25 padding to the start. For all intents and purposes this works as expected elsewhere in the program but not for my current use case. I understand that my issue may be with;

@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
    return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}

But after looking around there's very little information on how this works, and examples (apart from where I got the current calculation from which works as described above).

Dannys
  • 335
  • 1
  • 5
  • 12

0 Answers0