RecyclerView ItemDecoration-如何为每个viewHolder绘制不同的宽度分隔线?

时间:2019-06-25 09:58:43

标签: android android-recyclerview

当前我的分隔线仅绘制一种宽度:

enter image description here

如何为我的recyclerview中的每个增量位置添加一个额外的分隔线?

这是我的ItemDecoration类:

public SimpleDivider(Context mContext, ArrayList<Integer> mDepth) {
    mDivider = ContextCompat.getDrawable(mContext, R.drawable.recycler_view_divider);
    this.mContext = mContext;
    this.mDepth = mDepth;
    dividerMargin = 15;

}

@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) {
        int right = dividerMargin;
        int left = 0;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }

}

Edit1 :这是适配器。我以为不需要,因为所有逻辑都将写在ItemDecoration类中。

private ArrayList<String> mList;

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView singleMessageComment;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        singleMessageComment = itemView.findViewById(R.id.item_child_comment);
    }
}

public AdapterTest(ArrayList<String> mList) {
    this.mList = mList;
}

@NonNull
@Override
public AdapterTest.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_single_layout, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull AdapterTest.ViewHolder viewHolder, int i) {
    viewHolder.singleMessageComment.setText(mList.get(i));


}

@Override
public int getItemCount() {
    return mList.size();
}

1 个答案:

答案 0 :(得分:2)

添加装饰:

recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
recyclerView.addItemDecoration(new LeftDividerItemDecorator(this));

左分隔项装饰器的声明:

public class LeftDividerItemDecorator extends RecyclerView.ItemDecoration {
    private final Drawable mDivider;
    private final Rect mBounds = new Rect();
    private final Context mContext;

    LeftDividerItemDecorator(Context context) {
        mContext = context;
        mDivider = context.getResources().getDrawable(R.drawable.divider);
    }

    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (parent.getLayoutManager() != null && mDivider != null) {
            drawLeftDivider(c, parent);
        }
    }

    private void drawLeftDivider(Canvas canvas, RecyclerView parent) {
        canvas.save();

        int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; ++i) {
            View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, mBounds);

            int childAdapterPosition = parent.getChildAdapterPosition(child);

            int left = parent.getPaddingLeft();

            // Solid size according to divider.xml width
            //int right = left + (mDivider.getIntrinsicWidth());

            // Dynamic size according to divider.xml width multiplied by child number
            int right = left + (mDivider.getIntrinsicWidth() * (childAdapterPosition + 1));

            int top = child.getTop();
            int bottom = child.getBottom();

            // Draw left vertical divider
            mDivider.setBounds(
                    left,
                    top,
                    right,
                    bottom
            );

            mDivider.draw(canvas);
        }

        canvas.restore();
    }

    // Handles dividers width - move current views to right
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (mDivider == null) {
            outRect.set(0, 0, 0, 0);
        } else {
            int childAdapterPosition = parent.getChildAdapterPosition(view);
            outRect.set(mDivider.getIntrinsicWidth() * childAdapterPosition, 0, 0, 0);
        }
    }

}

分隔符的xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="4dp"
        android:height="4dp" />
    <solid android:color="@color/colorAccent" />
</shape>

预览:

enter image description here

相关问题