Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

android - Clicking on item programmatically in RecyclerView

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.

How can I perform this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Without your code we don't know how you implement your onClickListener.

Normally, you implement and set the item onClickListener in the viewHolder of your RecyclerView.Adapter.
For example: ViewHolder.itemView.setOnClickListener(listener);

So the solution is you can use recyclerView.findViewHolderForAdapterPosition(0).itemView.performClick()

PS: Do not use findViewHolderForLayoutPosition() according to the Doc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...