سؤال

I have an activity in a Google TV app, that displays 4 columns. The top-level widget in the layout is a horizontal LinearLayout, which contains the widgets that make up the columns. The 4 column widgets (from left to right) are: Vertical LinearLayout (containing buttons), ListFragment, ListFragment, Vertical LinearLayout (containing TextViews).

When populated with data, the 2 ListFragments are much taller than the visible height of the screen, and can be scrolled independently to move up/down on the list. This all works very well, except for some unwanted auto-scrolling on the ListFragments, when I move focus between columns.

For example, if I am focused at the top of the 2nd column (the left-most ListFragment) and I use the d-pad to move focus down to the middle of the visible screen, and then I push the right key on the d-pad (to move horizontally into the other ListFragment) the list item (in the 2nd ListFragment) that was alongside the focused item in the 1st ListFragment, receives focus (which is good) but at the same time the 2nd ListFragment auto-scrolls vertically, to push the newly focused item to the top of the visible screen (which is bad). Instead I want it to stay where it was (in the middle of the list).

The same problem happens if I move left from the middle of the 2nd ListFragment, back into the 1st one. It also happens if I move right from one of the buttons in the 1st column, into the middle of the 1st ListFragment.

I understand that this auto-scroll behavior may be desirable for some applications, but in my app its not really appropriate, and causes user confusion.

هل كانت مفيدة؟

المحلول

It seems like a bug in ListView d-pad navigation between parallel vertical fragments. Please try reproducing it on a tablet and file a bug in the Android issue tracker.

Meanwhile you could use the following workaround:

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // workaround to handle keyevent dpad-right from left list to right list, to 
        // to prevent default behavior of focused item on right list snapping to top.             
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if (left_list != null && right_list != null && left_list.isFocused()){
                right_list.setSelection(right_list_selected_index);
                right_list.requestFocus();
                return true;
            }
        } 
        return super.onKeyDown(keyCode, event);
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top