在 RecyclerView 中以编程方式单击项目 [英] Clicking on item programmatically in RecyclerView

查看:21
本文介绍了在 RecyclerView 中以编程方式单击项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载一个类别列表,根据选择的类别,我需要在 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 中以编程方式单击项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆