syed kashifullah
syed kashifullah

Reputation: 21

how to focus a specific item in recycler view?

I have 50 items in my recycler view. I want to focus or show specific item like when I click on button 1 it will focus items 10, and when I click on button 2 it will show/focus on items 30 and so on.

Upvotes: 1

Views: 2993

Answers (4)

Rob
Rob

Reputation: 1047

val itemPositionToFocus = 0 // for example the first item
val viewHolder = mRecyclerView.findViewHolderForLayoutPosition(itemPositionToFocus)
viewHolder.itemView.requestFocus()

Upvotes: 0

Jaydeep parmar
Jaydeep parmar

Reputation: 569

If you are looking for how to scroll to that 10th and 30th item then below solution is for you.

recyclerView.getLayoutManager().scrollToPosition(position)

Here position is, in which position you want to scroll.

For highlight that item you can change text format like bold or color of that item of recycler view.

Upvotes: 1

Haseeb Hassan Asif
Haseeb Hassan Asif

Reputation: 885

If you want to highlight a certain items, then pass another arraylist containing ids to be highlighted to your adapter. And create a function usually through an interface that updates that list and reloads all the items in a different manner. and for scroll use smoothScrollToPosition(position).

Upvotes: 0

sergpetrov
sergpetrov

Reputation: 1714

You can use the RecyclerView directly for that.

Scroll to item:

  • with animation
recyclerView.smoothScrollToPosition(itemPosition)
  • without animation
recyclerView.scrollToPosition(itemPosition)

Upvotes: 5

Related Questions