RecyclerView-创建项目分隔线时移动布局

时间:2019-06-24 09:04:07

标签: android android-recyclerview

我正在使用itemDecoration为我的每个viewHolders实现一个垂直分隔符。我该怎么做,以便布局首先绘制分隔线,然后将视图绘制到分隔线的右侧?

@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {



    int top = 0;
    int bottom = parent.getHeight();


    int childCount = parent.getChildCount();

    for(int i = 0; i < childCount; ++i) {
        View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, this.mBounds);
        int right = 150;
        int left = 0;
        this.mDivider.setBounds(left, top, right, bottom);
        this.mDivider.draw(c);
    }

}

1 个答案:

答案 0 :(得分:1)

您还需要覆盖getitemoffsets()。在此处将偏移量应用于项目:

@Override
public void getItemOffsets(
                Rect outRect,
                View view, 
                RecyclerView parent,
                RecyclerView.State state
) { outRect.left = 150; }