如何从左右两侧将ItemDecoration应用于RecyclerView项目?

时间:2018-08-27 12:53:48

标签: android android-recyclerview kotlin item-decoration

我一直在努力实现这一目标。我想要的是从左到右重叠所选的RecyclerView项目,如下图所示。

enter image description here enter image description here

我可以通过ItemDecoration向左或向右移动,如下所示:

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)
        }
    }
}

到目前为止,我已经实现了如下图所示的效果。 enter image description here

我已经尝试使用CarouselLayoutManager,但不是我想要的。

1 个答案:

答案 0 :(得分:0)

要获得所需的结果,您需要执行两个步骤:

首先,更正装饰器计算:

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

第二,您需要实际添加阴影

并且,对该类进行了一些快速的清理,您不需要private val overLapValue

相反:

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