美文网首页
RecyclerView添加Item间隙

RecyclerView添加Item间隙

作者: Leero丶 | 来源:发表于2020-04-21 16:54 被阅读0次
Kotlin
class SpacesItemDecoration(private var space: Int) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        if (parent.getChildLayoutPosition(view) != 0) {
            outRect.left = ScreenUtils.dip2px(view.context, space.toFloat())
        }
    }
}
Java
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public SpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;
        if (parent.getChildAdapterPosition(view) == 0) {
            outRect.top = space;
        }
    }
}

相关文章

网友评论

      本文标题:RecyclerView添加Item间隙

      本文链接:https://www.haomeiwen.com/subject/cahpihtx.html