looping-layout icon indicating copy to clipboard operation
looping-layout copied to clipboard

ScrollToNextItem function

Open yfsx opened this issue 5 years ago • 6 comments

:key: Use case

Hi, just wondering if the looper can implement scrollToNextItem() function

:hammer: Proposal

  • a function that be able to scroll to next item; input should be delay time until next item can be scrolled.
  • a function to stop next item scroll

:wrench: Alternative

:memo: Other info

:bee: Request for assignment

I'm just requesting this feature. I don't want to implement it.

yfsx avatar Feb 16 '21 10:02 yfsx

Hello, thank you for submitting this issue :D

Currently the LoopingLayoutManger supports scrolling via the smoothScrollToPosition function of the RecyclerView. If you use this in combination with the new adapter index accessors, and DirectionalDeciders I think you should be able to get the LoopingLayoutManager to do what you want.

Do you think this will work for your use case? =)

Best wishes --Beka

BeksOmega avatar Feb 16 '21 23:02 BeksOmega

Has anyone managed to solve this problem?

Ewrei avatar Aug 17 '22 15:08 Ewrei

Has anyone managed to solve this problem?

Have you tried the recommendation in my above comment?

BeksOmega avatar Aug 17 '22 15:08 BeksOmega

@BeksOmega I tried parsing, but I get a failure, most likely I'm doing something wrong(

Ewrei avatar Aug 18 '22 07:08 Ewrei

@BeksOmega I tried parsing, but I get a failure, most likely I'm doing something wrong(

Could you post the code you tried? If I see what you're doing I may be able to give advice =)

BeksOmega avatar Aug 18 '22 15:08 BeksOmega

I solved my problem by adding this extension.

fun LoopingLayoutManager.smoothSnapToPosition(
    context: Context,
    itemCount: Int,
    millisecondPerInch: Float = MILLISECONDS_PER_INCH,
    snapMode: Int = LinearSmoothScroller.SNAP_TO_START
) {
    val smoothScroller = object : LinearSmoothScroller(context) {
        override fun getVerticalSnapPreference(): Int = snapMode
        override fun getHorizontalSnapPreference(): Int = snapMode
        override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float =
            millisecondPerInch / displayMetrics.densityDpi
    }

    val nextPosition = this.topLeftIndex + 1

    Handler(Looper.getMainLooper()).postDelayed({
        smoothScroller.targetPosition = if (nextPosition == itemCount) 0 else nextPosition % itemCount
        startSmoothScroll(smoothScroller)
    }, DELAY_MILLIS)
}

Also thanks for such a cool library)

Ewrei avatar Aug 18 '22 15:08 Ewrei