WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

RecyclerView - Getting adapter position in ItemDecoration's onDraw method?

In itemDecoration's on draw method it has a override method called getItemOffsets which I can get the adapter's position using:

@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    int childCount = parent.getChildCount();
    int position = parent.getChildAdapterPosition(view);
}

How ever if I try this method inside onDraw:

@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
      int position = parent.getChildAdapterPosition(   ???   );
}

What do I pass for the view parameters?

Upvotes: 1

Views: 698

Answers (1)

deadfish
deadfish

Reputation: 12304

In onDraw() You are working on one canvas. To modify one child, You have to iterate by all the parent's children.

int childCount = parent.getChildCount();

for (int i = 0; i < childCount; ++i) {
    View child = parent.getChildAt(i);
    int childAdapterPosition = parent.getChildAdapterPosition(child);
    // ...
}

Upvotes: 2

Related Questions