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

查看:82
本文介绍了在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天全站免登陆