3

Here VerticalGridView(VG) will be going to fill with some data. The VG receives the focus on pressing D-PAD key up/down, it is working fine. But the problem is when the focus is at zeroth item in the grid and the user presses D-PAD keyup focus must be set one of the view outside of VG, which is ll_exit(id mentioned in xml file), but currently focus is not going to the said view. Kindly let me know how i to solve this problem.

i tried setting up android:nextFocusUp="@id/ll_exit" but it is not moving and tried in activity level to implement the onKeyUp method as below

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_UP:
                Log.d(TAG, "onKeyUp: " + event.getAction());
                GenresFragment fragment = (GenresFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_genre);
                int pos = fragment.getVerticalGridView().getSelectedPosition();
                if (pos == 0) {
                    fragment.getLlExit().requestFocus();
                }
                return true;
            default:
                return super.onKeyUp(keyCode, event);
        }
    }

but here the problem is when i'm at 1st item in VG and presses the key up button suddenly it'll go to the view that i need to focused. That is ll_exit view in xml file

<b>main_layout.xml</b>

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_toolbar"
            android:layout_width="@dimen/select_genre_fragment_width"
            android:layout_height="@dimen/select_genre_toolbar_height"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_all_channels"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <LinearLayout
                android:id="@+id/ll_genres"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <LinearLayout
                android:id="@+id/ll_exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>

        <android.support.v17.leanback.widget.VerticalGridView
            android:id="@+id/vg_genres"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/ll_toolbar"
            android:nextFocusUp="@id/ll_exit" />
    </RelativeLayout>
Shiv
  • 53
  • 1
  • 11
  • I have the same problem. Friend, did you manage to fix it? When I use RecyclerView, the focus does not work stably when scrolling, but it moves to other elements, but when using the VerticalGridView it feels like it blocks any movement outward. – kirkadev Aug 09 '21 at 08:16
  • 1
    yes @kirkadev i am able to fix by defining one custom class which will check for the next focus or previous focus – Shiv Aug 13 '21 at 04:56
  • a not found straight solution too. I got the position of the focused item, and if it is in the first row, then I call the requestFocus() method for the desired view. – kirkadev Aug 13 '21 at 05:20

3 Answers3

7

Try adding the following XML attributes to the VG

app:focusOutEnd="true"
app:focusOutFront="true"

it works for me !

Jack Jiao
  • 71
  • 1
  • 4
1

Try adding the following XML attributes to the LinearLayout view ll_exit

android:focusable="true"
android:focusableInTouchMode="true"
burakk
  • 1,231
  • 2
  • 22
  • 45
  • i tried by adding those attributes in xml file but it is of no use. The problem is focus is going from outside view to recycler view adapter but if i want to receive focus back to outside view on key up press it is not happening – Shiv May 13 '19 at 07:09
  • Put the code you posted in onKeyUp method into the onKeyDown method instead of onKeyUp. That might work. – burakk May 14 '19 at 23:35
0

Possible ways: try setting the LinearLayout ll_toolbar to

android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="afterDescendants"

Or try:

View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_UP);

if (nextFocused != null) {
    nextFocused.requestFocus();
}

If no solutions work just extend VerticalGridView like this

public class SampleVerticalGridView extends VerticalGridView implements FocusInterceptor{


    private static final String TAG = "SampleVerticalGridView";
    private View focusUp;
    private View focusDown;

    // constructors and stuff
    ...

    public void setNextFocusViews(View focusUp, View focusdown) {
        this.focusUp = focusUp;
        this.focusdown = focusdown;
    }

    @Override
    public View focusSearch(View focused, int direction) {
        if (direction == View.FOCUS_UP && focusUp != null) {
            return focusUp;
        } else if (direction == View.FOCUS_DOWN && focusDown != null) {
            return focusDown;
        }
        return super.focusSearch(focused, direction);
    }

    ...
}
ReDLaNN
  • 484
  • 4
  • 14