h6ah4i / android-advancedrecyclerview

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
https://advancedrecyclerview.h6ah4i.com/
Apache License 2.0
5.32k stars 862 forks source link

Unable to get correct packed position in RecyclerView.ItemDecoration onDraw #356

Open zeroarst opened 7 years ago

zeroarst commented 7 years ago

I am trying draw borders for groups. I found that the views' orders in the RecyclerView are not always right.

Assume there are 3 album groups, the second one is expanded, 3 items are showing, and I click second one to collapse, observer what is in RecyclerView.ItemDecoration onDraw. In the list, in theory, the order should be: (In parenthesis, g is getPackedPositionGroup, c is getPackedPositionChild).

  1. Album group 1 (g: 1)
  2. Album group 2 (g: 2)
  3. Album group 2 item 1 (g: 2, c: 1)
  4. Album group 2 item 2 (g: 2, c: 2)
  5. Album group 2 item 3 (g: 2, c: 3)
  6. Album group 3 (g: 3)

However, in reality, I see

  1. Album group 1 (g: 1)
  2. Album group 2 (g: 2)
  3. Album group 3 (g: 3)
  4. Album group 2 item 3 (g: 2, c: -1)
  5. Album group 2 item 2 (g: 2, c: -1)
  6. Album group 2 item 1 (g: 2, c: -1)

No order is weird, which I don't know why. but does not matter in my question. My question is that I cannot get correct child position when collapsing. I still can get correct group position against those children items. I wonder is there any way to still get the right child position in this case? I know that I still can call RecyclerView.getChildLayoutPosition to get the right position, because the changes not reflects to adapter yey. But this does not help. I need to know children's position in the group. Maybe a a method like getPackedLayoutPositionChild?

Actually I use another way to get children positions, I store the position in the ViewHolder when binding data. Just wondering if there any existing way to do it.

h6ah4i commented 7 years ago

Actually I use another way to get children positions, I store the position in the ViewHolder when binding data. Just wondering if there any existing way to do it.

Hi. Unfortunately, there is no existing way to handle this kind of problem. I think your solution is correct, I would do the same thing if I need to implement group/child position specific item decorations.

TreyWurm commented 5 years ago

Hi, currently I am facing the same problem and was wondering how to access those positions stored in the ViewHolder from within the Decorator?

TreyWurm commented 5 years ago

Nevermind, already found the solution. Wasn't looking into RecyclerView. Now found its method getChildViewHolder(view)