我正在加载一个类别列表,根据选择的类别,我需要在 RecyclerView 中显示相应类别的产品.因此,当我第一次启动 Fragment 时,我需要模拟用户点击默认类别 - 在这种情况下,它将是第一个.
I am loading a list of categories and depending on which is selected, I need to show the products of the corresponding category in the RecyclerView. So when I first start the Fragment I need to simulate the user clicking in a default category - in this case, it would be the first one.
我该如何执行此操作?
推荐答案没有你的代码,我们不知道你如何实现你的 onClickListener.
Without your code we don't know how you implement your onClickListener.
通常,您在 RecyclerView.Adapter 的 viewHolder 中实现并设置 onClickListener 项.例如:ViewHolder.itemView.setOnClickListener(listener);
Normally, you implement and set the item onClickListener in the viewHolder of your RecyclerView.Adapter. For example: ViewHolder.itemView.setOnClickListener(listener);
所以解决方案是你可以使用 recyclerView.findViewHolderForAdapterPosition(0).itemView.performClick()
So the solution is you can use recyclerView.findViewHolderForAdapterPosition(0).itemView.performClick()
PS:不要根据findViewHolderForLayoutPosition()>文档
PS: Do not use findViewHolderForLayoutPosition() according to the Doc
更多推荐
在 RecyclerView 中以编程方式单击项目
发布评论