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) {
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) {
But does not work. Can anybody give any advice?
Thanks in advance!