在 RecyclerView 中以编程方式单击项目

编程入门 行业动态 更新时间:2024-10-26 02:28:14
本文介绍了在 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 中以编程方式单击项目

本文发布于:2023-11-13 01:07:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   单击   方式   项目   RecyclerView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!