Reputation: 2564
I've set a recyclerview that works fine. I want to know when some element have the focus in the screen. My recyclerview display every item to fullscreen. Is possible to know, in the viewholder or in some place, what's the item that have the focus?
My problem is that I have a countdown in every item. And I only want that the countdown start when the item has the focus.
Upvotes: 1
Views: 3064
Reputation: 126
there is no object supposed to "have" the focus.
If you want that feature, you need to implement it.
For example, you can add a member hasFocus
in your model class, then in your view holder,
if (hasFocus) displayWithFocus();
else displayWithoutFocus();
Then you must define a view.OnItemClicekListener like this
onItemClicked(... View view, position p, ...){
itemsList.elementAt(p).setHasFocus(true)
}
This way you can also define if you want one item or several items to get selected.
To get to know who "has" the focus (is selected), you must iterate through the items list (model side).
edit: focus could only be used for views traversal, or use for example with a keyboard ...
Upvotes: 0
Reputation: 2186
Try calling getFocusedChild():
View focusedView = (View) recyclerview.getFocusedChild();
Upvotes: 2