5

I have been trying to achieve this for so long. What I want is to overlap the selected RecyclerView item from left and right as shown in the picture below.

enter image description here enter image description here

I'm able to achieve left or right by ItemDecoration like below:

class OverlapDecoration(private val overlapWidth:Int) : RecyclerView.ItemDecoration() {
    private val overLapValue = -40

    val TAG = OverlapDecoration::class.java.simpleName

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {

        val itemPosition = parent.getChildAdapterPosition(view)

        if (itemPosition == 0) {
            return
        } else {
            outRect.set(overLapValue, 0, 0, 0)
        }
    }
}

I have achieved like below image so far. enter image description here

I have already tried with CarouselLayoutManager but it not what I'm looking for.

Maddy
  • 4,525
  • 4
  • 33
  • 53

1 Answers1

-1

To achieve the result you're looking for you need to take two steps:

First, to correct the decorator calculations:

if (itemPosition == 0) {
    return
} else {
    outRect.set(-1 * overLapValue, 0, overLapValue, 0) //Need left, AND right
}

Second, you need to actually add the shadow

And, one quick bit of cleanup for the class, you don't need the private val overLapValue.

Instead:

class OverlapDecoration(private val overlapWidth:Int = 40) : RecyclerView.ItemDecoration() {
gtcompscientist
  • 671
  • 5
  • 18
  • Thanks, but this is not what I want! It will only increase the space from the right item. Actually, I want the right item to go under the selected item in RecyclerView. – Maddy Nov 28 '18 at 04:54