user3820641 user3820641 - 10 months ago 32
Android Question

Leanback focus problems

Hello guys I'm working on application and my layout structure is as following :

RelativeLayout:

CompoundView:

CompoundView:
RelativeLayout:
Button
Button
RecyclerView

BrowseFragment:
Only rows


My problem is when I get to first row of browse fragment and first item in it and I want to go up (D-PAD-UP) to focus button it does nothing it works only when I push left ( D-PAD-LEFT). Anyone has solution for this ?

Answer Source

So the problem was in BrowseFrameLayout for some reason and to solve this issue I had to override onFocusSearchListener and manage focus myself.

In BrowseFragment which I extended I have this method :

public void workaroundFocus(){
    if(getView() != null) {
        View viewToFocus  = getActivity().findViewById(R.id.view_to_focus);
        BrowseFrameLayout browseFrameLayout = getView().findViewById(android.support.v17.leanback.R.id.browse_frame);
        browseFrameLayout.setOnFocusSearchListener((focused, direction) -> {
            if (direction == View.FOCUS_UP) {
                return viewToFocus;
            }
            else {
                return null;
            }
        });
    }
}

And then:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    workaroundFocus();
    /*
      Rest of the code
    */
}

And voila it works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download