title: How to get GridView scroll to top using keyboard in Android TV
body: <p>I am developing apps on Android smart TV, which can only use remote controller to navigate the focus position(just like using d-pad key in normal android devices). </p>
  
  <p>I used <code>GridView</code> to display images. The problem is when scrolled to bottom, it cannot be scrolled up any more! I know this phenomena will never happen in touch mode device, but for Android TV it is really common.</p>
  
  <p>Actually, I faced the same problem when I using <code>ListView</code>, and I solved it by scrolling to the upper row manually. Override <code>AbsListView.OnScrollListener</code>:</p>
  
  <pre><code>@Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
          int totalItemCount) {
      try {
          if (firstVisibleItem &gt; 0 
                  &amp;&amp; view.getChildAt(0).isSelected()
                  || view.getChildAt(0).hasFocus()) {
              view.smoothScrollToPosition(firstVisibleItem - 1);
              Log.d(TAG, "scroll up");
          } else if ((firstVisibleItem + visibleItemCount) &lt; totalItemCount
                  &amp;&amp; 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);
      }
  }
  </code></pre>
  
  <p>As you know, there is a BIG difference between <code>ListView</code> and <code>GridView</code>, that is <code>GridView</code> has mulit-column items. So, when focus on the first row(not the first row of adapter data), the focused item not always be the <code>firstVisibleItem</code>, and the code above not work anymore.</p>
  
  <p><strong>EDIT:</strong> The reason why <code>ListView</code> and <code>GridView</code> cannot scroll up is the mount height of  all visible items exactly equals their parent's height. When D-UP key press, the focus move to the widget above <code>GridView</code> instead of the upper items.</p>
  
  <p>I tried this:</p>
  
  <pre><code>@Override
  public void onScroll(AbsListView view, int firstVisibleItem,
          int visibleItemCount, int totalItemCount) {
      if (firstVisibleItem &gt; 0 &amp;&amp; isFocusOnFirstRow()) {
          view.smoothScrollToPosition(firstVisibleItem - 1);
          Log.d(TAG, "scroll up");
      }
  }
  
  private boolean isFocusOnFirstRow() {
      try {
          for (int i = 0; i &lt; mGridView.getNumColumns(); i++) {
              if (mGridView.getChildAt(i).hasFocus()) {
                  return true;
              }
          }
      } catch (Exception e) {
          // ArrayIndexOutofBoundsException
          Log.e(TAG, e.getMessage());
      } 
      return false;
  }
  </code></pre>
  
  <p>But not working. Can anybody give some advices? </p>
  
  <p>Thanks in advance!</p>