简体   繁体   English

如何通过项目的参数在回收适配器中获取项目 position?

[英]How to get item position in recycler adapter by item's parameter?

I have two Fragments.我有两个片段。 In Fragment A I choose item with its unique ID.Fragment A ,我选择具有唯一 ID 的项目。 In Fragment B I have recycler adapter that consists of those items.Fragment B中,我有由这些项目组成的回收器适配器。 I would like to take item from Fragment A, and open Fragment B with recycler adapter set to the position of chosen item .我想从 Fragment A 中取出物品,然后打开 Fragment B,并将回收器适配器设置为所选物品的 position I found that in order to open recycler adapter on specific spot, I have to use something like that rv.getLayoutManager().scrollToPosition(positionInTheAdapter).我发现为了在特定位置打开回收器适配器,我必须使用类似rv.getLayoutManager().scrollToPosition(positionInTheAdapter). The question is: can I get that position by item's id?问题是:我可以通过项目的 id 得到 position 吗? Or maybe there's a better way of what I'm trying to achieve.或者也许有更好的方法来实现我想要实现的目标。

Thank you谢谢

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)您必须传递片段 B 中的项目列表。现在,当您 select 第一个片段 A 中的任何项目时,您需要做的就是循环遍历传递给适配器的所有项目列表并匹配 position 项目 id 匹配然后可以将 position 传递给rv.getLayoutManager().scrollToPosition(positionInTheAdapter)

For more info on how to pass data among fragments you can check this article.有关如何在片段之间传递数据的更多信息,您可以查看这篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM