fix tab switcher close/refresh issue

    Bug: 5015810
    Also fixes the case when closing a tab without changing the
    selected position the list would be empty.

Change-Id: Ibf4de0b33c696d14e15b82c07e697b1f1736565a
diff --git a/src/com/android/browser/NavScreen.java b/src/com/android/browser/NavScreen.java
index 1c35b45..6a51e87 100644
--- a/src/com/android/browser/NavScreen.java
+++ b/src/com/android/browser/NavScreen.java
@@ -156,8 +156,11 @@
 
     private void onCloseTab(Tab tab) {
         if (tab != null) {
-            switchToSelected();
-            mUiController.closeCurrentTab();
+            if (tab == mUiController.getCurrentTab()) {
+                mUiController.closeCurrentTab();
+            } else {
+                mUiController.closeTab(tab);
+            }
             mAdapter.notifyDataSetChanged();
         }
     }
@@ -229,8 +232,10 @@
                 @Override
                 public void onClick(View v) {
                     if (tabview.isClose(v)) {
-                        onCloseTab((Tab) (mScroller.getSelectedItem()));
+                        onCloseTab(tab);
                     } else if (tabview.isTitle(v)) {
+                        mScroller.setSelection(position);
+                        switchToSelected();
                         mUi.getTitleBar().setSkipTitleBarAnimations(true);
                         close(false);
                         mUi.editUrl(false);
@@ -238,7 +243,6 @@
                     } else if (tabview.isWebView(v)) {
                         mScroller.setSelection(position);
                         close();
-
                     }
                 }
             });
diff --git a/src/com/android/browser/view/Gallery.java b/src/com/android/browser/view/Gallery.java
index 5ab78b4..aa6ed65 100644
--- a/src/com/android/browser/view/Gallery.java
+++ b/src/com/android/browser/view/Gallery.java
@@ -224,6 +224,7 @@
                 // Nothing selected
                 checkSelectionChanged();
             }
+            layout(0, false);
         } else {
             // checkFocus();
             mOldSelectedPosition = INVALID_POSITION;
@@ -231,6 +232,7 @@
             resetList();
             // Nothing selected
             checkSelectionChanged();
+            invalidate();
         }
     }
 
@@ -240,7 +242,6 @@
     void resetList() {
         mDataChanged = false;
         removeAllViewsInLayout();
-        invalidate();
     }
 
     public void setCallbackDuringFling(boolean shouldCallback) {