Home > Blockchain >  How to get item position in recycler adapter by item's parameter?
How to get item position in recycler adapter by item's parameter?

Time:12-29

I have two Fragments. In Fragment A I choose item with its unique ID. In Fragment B I have recycler adapter that consists of those items. I would like to take item from Fragment A, and open Fragment B with recycler adapter set to the position of chosen item. I found that in order to open recycler adapter on specific spot, I have to use something like that rv.getLayoutManager().scrollToPosition(positionInTheAdapter). The question is: can I get that position by item's id? Or maybe there's a better way of what I'm trying to achieve.

Thank you

CodePudding user response:

You must be passing a list of items in Fragment B. Now when you select any item in first Fragment A then all you need to do is run a loop through all list of items passed to adapter and match on which position the item id matches that position can then be passed to rv.getLayoutManager().scrollToPosition(positionInTheAdapter)

For more info on how to pass data among fragments you can check this article.

  • Related