How to programmatically snap to position on screen of Recycler using LinearSnapHelper

I have implemented a horizontal recyclerView with LinearSnapHelper to implement a UI that picks a specific configuration. Kinda, as a vintage school picker / selector or spinner. The item in the center is the selected position.

it works fine and that's it, but here's the problem. On initial launch, I need to programmatically set the position of the recycler view so that the selected item (whose index was loaded from disk) is in the center.

.scrollToPosition()

doesn't work because it puts the selected item at the beginning.

now I know I can do all the math and calculate the x coordinate and manually set it, but this is a lot of redundant work because LinearSnapHelper already does this and I feel like there should be a way to just reuse this logic, but with actually triggering the cast ...

I need something like LinearSnapHelper.snapToPosition()

+9


source to share


3 answers


More general solution:

  1. First, scroll through the RecyclerView to make the target item visible.
  2. Then take the target view object and use the SnapHelper to determine the distance for the final shot.
  3. Finally, scroll to the target position.

NOTE . This only works because programmatically you scroll to the exact position and overlap the missing distance by the exact value, using scrollBy

smooth scrolling instead.



Snippet of code :

mRecyclerView.scrollToPosition(selectedPosition);
mRecyclerView.post(() -> {
        View view = mLayoutManager.findViewByPosition(selectedPosition);
        if (view == null) {
            Log.e(WingPickerView.class.getSimpleName(), "Cant find target View for initial Snap");
            return;
        }

        int[] snapDistance = mSnapHelper.calculateDistanceToFinalSnap(mLayoutManager, view);
        if (snapDistance[0] != 0 || snapDistance[1] != 0) {
            mRecyclerView.scrollBy(snapDistance[0], snapDistance[1]);
        }
    }
});

      

+9


source


Try to call smoothScrollToPosition on the RecyclerView and pass the position index (int)

mRecyclerView.smoothScrollToPosition(position);

      



Worked for me with LinearLayoutManager and LinearSnapHelper. It will animate the initial scroll, but at least anchor the element to the desired position.

This is my first post on the stack, hope it helps :)

0


source


I have recyclerView

one that I added padding

to left

and right

with dummy views in the adapter. So the first "actual" element can be bound to.

I couldn't get it smoothScrollToPosition(0)

to work though for the initial binding. I have used the following

recycler.scrollBy(snapHelper.calculateDistanceToFinalSnap(binding.recycler.getLayoutManager(), recycler.getChildAt(1))[0], 0);

      

Not the prettiest view, but it seems to work!

-1


source







All Articles