androidandroid-recyclerviewandroid-tvd-pad

Navigating in View with RecyclerView


I have application supporting both Android TV and Smartphones

In general navigation through app goes fine, but I have one problem with layout like this

View with Buttons
RecyclerView
View with Buttons

I want navigate across this view with D-pad (or arrows) but Recycler is kinda problem.

I can set focus on recycler from upper and bottom view by setting

android:nextFocusDown="@+id/recycler"  

and making recycler focusable, but now I want "go in" recycler for example with D-pad OK (or Enter)

how can I move focus from recycler to first child of recycler on imeInputType (ok/enter) and reset focus on clicking (back/esc) from child back to his parent recycler?


Solution

  • You are adding child views programmatically, so you need to set programmatically your next focused view.

    Something like this

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
    
                View child = mRecyclerView.getLayoutManager().getChildAt(0);
                child.setFocusable(true);
                child.setFocusableInTouchMode(true);
                child.requestFocus();
    
                // to check current focused view
                // View focused = getActivity().getCurrentFocus();
    
                return true;
            }
            return false;
        }
    });