How to programmatically snap to position on Recycler view with LinearSnapHelper

前端 未结 3 1061
执念已碎
执念已碎 2021-02-07 05:40

I have implemented a horizontal recyclerView with LinearSnapHelper, to implement a UI input that selects a particular configuration. Kinda like the old school number picker/sele

3条回答
  •  醉梦人生
    2021-02-07 06:35

    I have a recyclerView which I have added padding at the left and right with dummy views in the adapter. So that the first "actual" item can be snapped to.

    I couldn't get smoothScrollToPosition(0) to work though for the initial snap. I used the following

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

    Isn't the nicest looking way, but seems to work!

提交回复
热议问题