Muhammad Abdullah
Muhammad Abdullah

Reputation: 137

How to scroll discretely in recylcer view with gridlayoutmanager?

I'm using a recyclerView with GridLayoutManager and I've set it to scroll horizontally. i want it to scroll discretely. This is how its displaying when i launch it.

Current display

and when i scroll it even a little bit it should keep scrolling until the next item is completely visible aka discretely and not stop before that . it should look like this.

like this

and it should end like this. Not like this

How can i scroll discretely like that ? Any guidance or help would be highly appreciated!!

Upvotes: 0

Views: 277

Answers (1)

Taha wakeel
Taha wakeel

Reputation: 169

I have used SnapHelper in my project, just make a class in your util package and do like this.

public class MySnapHelper extends LinearSnapHelper {

private OrientationHelper verticalHelper, horizontalHelper;

public MySnapHelper() {

}

@Override
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
        throws IllegalStateException {
    super.attachToRecyclerView(recyclerView);
}

@Override
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
                                          @NonNull View targetView) {
    int[] out = new int[2];

    if (layoutManager.canScrollHorizontally()) {
        out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
    } else {
        out[0] = 0;
    }

    if (layoutManager.canScrollVertically()) {
        out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager));
    } else {
        out[1] = 0;
    }
    return out;
}

@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {

    if (layoutManager instanceof LinearLayoutManager) {

        if (layoutManager.canScrollHorizontally()) {
            return getStartView(layoutManager, getHorizontalHelper(layoutManager));
        } else {
            return getStartView(layoutManager, getVerticalHelper(layoutManager));
        }
    }

    return super.findSnapView(layoutManager);
}

private int distanceToStart(View targetView, OrientationHelper helper) {
    return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}

private View getStartView(RecyclerView.LayoutManager layoutManager,
                          OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();

        boolean isLastItem = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
                && helper.getDecoratedEnd(child) > 0) {
            return child;
        } else {
            if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1) {
                return null;
            } else {
                return layoutManager.findViewByPosition(firstChild + 1);
            }
        }
    }

    return super.findSnapView(layoutManager);
}

private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager) {
    if (verticalHelper == null) {
        verticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
    }
    return verticalHelper;
}

private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager) {
    if (horizontalHelper == null) {
        horizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
    }
    return horizontalHelper;
}

}

and then just attach it with your recyclerview like this:

SnapHelper mySnapHelper = new MySnapHelper ();
mySnapHelper.attachToRecyclerView(mRecyclerView);

Upvotes: 1

Related Questions