RecyclerView ItemDecoration - How to draw a different width divider for every viewHolder?

拟墨画扇 提交于 2021-02-19 06:00:29

问题


Currently my divider is only drawing one width:

How would can I add an extra divider for every increment position in my recyclerview?

Here is my ItemDecoration class:

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: Here's the Adapter. I thought it wouldn't be needed because all the logic would be written inside the ItemDecoration class.

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:


Adding decorations:

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

Declaration of the left divider item decorator:

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);
        }
    }

}

Divider's 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>

Preview:



来源:https://stackoverflow.com/questions/56751398/recyclerview-itemdecoration-how-to-draw-a-different-width-divider-for-every-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!