0

I got a recyclerview. I want to implement a carousel effect with fixed Y-Axis like this:

Good Carousel effect

I wrote a custom LinearLayoutManager to be able to modify the recyclerview scrolling behaviour.

I try to get an horizontal scroll with Y-Axis fixed to the bottom of the view and X-Axis to the center of the view.

private val mShrinkAmount = 0.30f
private val mShrinkDistance = 1f

override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
    super.onLayoutChildren(recycler, state)
    scrollHorizontallyBy(0, recycler, state)
}

override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
    val scrolled = super.scrollHorizontallyBy(dx, recycler, state)
    val scalePoint = (width / 2f)
    val d0 = 0f
    val d1 = (mShrinkDistance * scalePoint)
    val s0 = 1f
    val s1 = (1f - mShrinkAmount)

    for (index in 0..(childCount - 1)) {
        val child = getChildAt(index)
        val childMidPoint = ((getDecoratedLeft(child!!) + getDecoratedRight(child)) / 2f)
        val d = Math.min(d1, Math.abs(scalePoint - childMidPoint))
        val scale = (s0 + (s1 - s0) * (d - d0) / (d1 - d0))

        child.scaleX = scale
        child.scaleY = scale
        child.pivotY = (getDecoratedBottom(child) * 1f)
    }
    return scrolled
}

I set the value of pivotY to the bottom of the view here

child.pivotY = (getDecoratedBottom(child) * 1f)

But the result is not clean as you can see here:

Bad Carousel effect

The bottom of the views are not aligned and I got different padding on the right and left side when scrolling.

What did I miss?

1 Answers1

0

You shouldn't have to do this manually, there's a SnaperHelper class you can use Just do it like so:

val snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

More info here RecyclerView horizontal scroll snap in center

and here https://blog.mindorks.com/using-snaphelper-in-recyclerview-fc616b6833e8

Benoit TH
  • 601
  • 3
  • 12
  • Thanks, but not sure you understand the problem. How can I set the Y Pivot to the recyclerview bottom with a SnapHelper ? – Valentin Nasraty Mar 12 '19 at 17:03
  • Pivot is a value between 0 and 1, 0 being top and 1 being bottom so you should just have to set the pivot to 1 on your RecyclerView – Benoit TH Mar 12 '19 at 17:10
  • I tried to set the pivot (0 or 1) in the recyclerview and in the layoutManager, but it didn't work. The only way to get the pivot fixed to the bottom is `child.pivotY = (getDecoratedBottom(child) * 1f)` in the layoutManager. But I got a weird result. The space between images is not the same on the left and right side – Valentin Nasraty Mar 12 '19 at 17:26