AndroidTV RecyclerView: Highlight entry when using DPAD

710 Views Asked by At

This subject is driving my nuts. I've read virtually a hundred posts about it, but none of them reflects my situation. I'm using a plain simple RecyclerView in an app running on AndroidTV. To enable navigation, I've set

android:focusable="true"

Now, I can use DPAD to scroll inside the RecyclerView, nicely. My goal is to animate the highlighting of the currently focused item in the list. However, I can't seem to find any event which indicates a focus change.

I would very much appreciate a hint, how my code could be informed about a focus changed inside the list, programaticaly, and how to figure out which list items have gained/lost focus.

Thanks!

1

There are 1 best solutions below

0
Harry Developer On

Finally, it turned out that the solution is fairly easy. I don't know, why it was hidden in front of my eyes. All I had to do was to view.setOnFocusChangeListener() inside onBindViewHolder like this:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ...
    view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            highlightEntry(view, b);
        }
    });
    ...
}