How to make GridView scroll up using keyboard in Android TV

I am developing applications on an Android smartphone that can only use the remote control to navigate the focal position (just like using the d-pad key on regular Android devices).

I used GridView to display images. The problem is scrolling down, it cannot be scrolled! I know that this phenomenon will never happen in touch mode, but for Android TV it is really common.

Actually, I ran into the same problem when using ListView , and solved it by scrolling the top line manually. Override AbsListView.OnScrollListener :

 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { try { if (firstVisibleItem > 0 && view.getChildAt(0).isSelected() || view.getChildAt(0).hasFocus()) { view.smoothScrollToPosition(firstVisibleItem - 1); Log.d(TAG, "scroll up"); } else if ((firstVisibleItem + visibleItemCount) < totalItemCount && view.getChildAt(visibleItemCount-1).hasFocus() || view.getChildAt(visibleItemCount-1).isSelected()) { view.smoothScrollToPosition(firstVisibleItem + visibleItemCount + 1); Log.d(TAG, "scroll down"); } } catch (Exception e) { //Log.e(TAG, e.getMessage(),e); } } 

As you know, there is a BIG difference between ListView and GridView , i.e. GridView has mulit-column elements. Therefore, when you focus on the first line (and not on the first line of adapter data), the focused element will not always be firstVisibleItem , and the above code no longer works.

EDIT: The reason the ListView and GridView cannot scroll up is because the mounting height of all visible elements is exactly equal to their parent height. When the D-UP key is pressed, the focus will move to the widget above the GridView instead of the top elements.

I tried this:

 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem > 0 && isFocusOnFirstRow()) { view.smoothScrollToPosition(firstVisibleItem - 1); Log.d(TAG, "scroll up"); } } private boolean isFocusOnFirstRow() { try { for (int i = 0; i < mGridView.getNumColumns(); i++) { if (mGridView.getChildAt(i).hasFocus()) { return true; } } } catch (Exception e) { // ArrayIndexOutofBoundsException Log.e(TAG, e.getMessage()); } return false; } 

But does not work. Can anybody give any advice?

Thanks in advance!

+4
source share
1 answer

I have an idea that might work for you. If, instead of scrolling through the grid view, you simply destroy it and recreate it when you want to move to a vertex that can work. However, if you want to allow the user to scroll through it, a different approach is required. I recommend that you shift the view of the grid so that its elements are not completely aligned with the parent. For example, if you had a lower visible line that was only half displayed (cut off halfway), it tells the user that there are too many elements to scroll through. The same applies when scrolling up - the top line will only be half displayed (the top of the cropped items). This is a pattern that I see a lot for television applications.

0
source

Source: https://habr.com/ru/post/1491264/


All Articles