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

android - 如何将左右两侧的ItemDecoration都应用于RecyclerView项目?-LMLPHP
android - 如何将左右两侧的ItemDecoration都应用于RecyclerView项目?-LMLPHP

我可以通过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)
        }
    }
}

到目前为止,我已经实现了如下图所示的效果。
android - 如何将左右两侧的ItemDecoration都应用于RecyclerView项目?-LMLPHP

我已经尝试过CarouselLayoutManager了,但这不是我想要的。

最佳答案

为了获得想要的结果,您需要采取以下两个步骤:

首先,更正装饰器计算:

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() {

10-07 15:43