简体   繁体   中英

Nudge a paged recyclerview to hint at next page without switching

I'm using a horizontal recyclerview with a LinearLayoutManager and a PagerSnapHelper and each viewholder/page is fullscreen and snaps to the window so only one page is viewable once settled. Like most fullscreen swipeable tabs.

When inserting a new page AFTER the current page, I want to temporarily show/hint at it's presence without snapping/scrolling to it ie so the user knows they opened a new page and it's there if they want to view it without forcing them to actually view it.

So it would be the same effect if you were to start dragging your finger a tiny bit to expose the next page before releasing and letting it snap back to the starting page.

How can I do this? Thanks

I haven't done it with a RecyclerView yet, but I managed to do something similar on a ViewPager. Basically, you could use a ValueAnimator with a CycleInterpolator to scroll the RecyclerView each frame by a little bit and then back. Since it's scroll position is updated with each frame, I think this shouldn't collide with the snapping:

val startX = recyclerView.getScrollX().toFloat()
    val endX = startX + Utils.dpToPx(context, 100) // the amount you want to scroll in pixels


    val animator = ValueAnimator.ofFloat(1f, 0f)
    animator.cancel()
    animator.interpolator = CycleInterpolator(0.5f)
    animator.startDelay = 600
    animator.duration = 600
    animator.addUpdateListener { animation ->
        val alpha = animation.animatedValue as Float
        recyclerView.scrollTo((startX * alpha + (1f - alpha) * endX).toInt(), 0)
    }
    animator.start()

This works on a ViewPager and should just as well on a RecyclerView, since it has the same scrolling methods available...

recyclerView.smoothScrollBy(nudgeDistanceInPx, 0);

在PagerSnapHelper插入之前,它将平滑滚动以显示下一页并将其返回到其起始位置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM