如何将左右两侧的ItemDecoration都应用于RecyclerView项目? [英] How to apply ItemDecoration from left and right side both to RecyclerView item?

查看:55
本文介绍了如何将左右两侧的ItemDecoration都应用于RecyclerView项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

推荐答案

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

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

首先,要纠正装饰器的计算:

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

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

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

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

相反:

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

这篇关于如何将左右两侧的ItemDecoration都应用于RecyclerView项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆