Implementation of Android TV List focus memory

In the development of Android TV, we often have to deal with the focus. A common requirement is to have the focus memory function. When the focus moves to an item in the list, the focus moves out, and when it comes back, the focus should be located on the original item. For this requirement, the common implementation method is to use listview or recyclerview to implement the list, Maintain a variable to store the last focus position, maintain this variable in focus change or key event, and use this variable to locate

Concrete implementation

For example, when using recyclerview to implement the list, in the key event of each ItemView, judge whether the focus is moved outward according to the direction of the key and the position of the current view. If so, set the focus mode of the parent view, that is, recyclerview, and save the position information of the current view

When the user moves back to the focus, because the focus mode was set to block in the previous step, the parent view will first obtain the focus and increase the event processing of focus change:

As shown in the code, in the focus change processing, set the focus mode of the view to enable the sub view to obtain the focus; Get the position information saved in the previous step to manually set the focus

Another simpler method is introduced later, which has better encapsulation and does not need external logic for maintenance

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>