RecyclerView以编程方式单击 [英] RecyclerView programmatically click

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

问题描述

我正在尝试以编程方式单击recyclerView的项目.我正在使用:

I'm trying to programmatically click on an item of a recyclerView. I'm using:

recyclerView.findViewHolderForAdapterPosition(index).itemView.performClick();

当索引属于可见项目时,这非常有用.如果该物品不可见(在回收站视图的最后一个位置,是等距的),则引发Exception.

This perfectly works when the index is of a visible item. If the item is not visible (at the last position of the recyclerview, for istance), an Exception is thrown.

我该怎么办?

推荐答案

您可以直接调用onClick,前提是该视图管理自己的点击侦听器.

You could call onClick directly, assuming that view manages its own click listener.

View view = recyclerView.findViewHolderForAdapterPosition(index).itemView;
view.onClick(view);

如果点击侦听器位于其他位置,则只需使用onClick方法获取对该对象的引用,然后以正确的视图作为参数进行调用.

If the click listener is located somewhere else, you just need to get a reference to the object with the onClick method and call it with the correct view as a parameter.

这篇关于RecyclerView以编程方式单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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