Bug 1270068 - TopSitesGridAdapter: Implement getItemId() so that Android can recycle views appropriately. r=grisha draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Tue, 07 Jun 2016 14:34:36 +0200
changeset 376119 f258f7074c2974f6f56bd59c30fe76e09767f1ea
parent 376118 d6e0d9a0319f4d52cfdb15b691b50b4e1bd36079
child 523083 d172fa1ffd83be092c5967373c6cdb7447737875
push id20504
push users.kaspari@gmail.com
push dateTue, 07 Jun 2016 12:36:21 +0000
reviewersgrisha
bugs1270068
milestone50.0a1
Bug 1270068 - TopSitesGridAdapter: Implement getItemId() so that Android can recycle views appropriately. r=grisha MozReview-Commit-ID: 5mwPOTcIRRs
mobile/android/base/java/org/mozilla/gecko/home/TopSitesPanel.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/TopSitesPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/TopSitesPanel.java
@@ -581,16 +581,35 @@ public class TopSitesPanel extends HomeF
                 // thumbnails if necessary.
                 gridItem.markAsDirty();
             }
 
             notifyDataSetChanged();
         }
 
         @Override
+        public long getItemId(int position) {
+            // We are trying to return stable ids so that Android can recycle views appropriately:
+            // * If we have a history id then we return it
+            // * If we only have a bookmark id then we negate it and return it. We negate it in order
+            //   to avoid clashing/conflicting with history ids.
+
+            Cursor cursor = getCursor();
+            cursor.moveToPosition(position);
+
+            final long historyId = cursor.getLong(cursor.getColumnIndex(TopSites.HISTORY_ID));
+            if (historyId != 0) {
+                return historyId;
+            }
+
+            final long bookmarkId = cursor.getLong(cursor.getColumnIndex(TopSites.BOOKMARK_ID));
+            return -1 * bookmarkId;
+        }
+
+        @Override
         public void bindView(View bindView, Context context, Cursor cursor) {
             final String url = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.URL));
             final String title = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.TITLE));
             final int type = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.TYPE));
 
             final TopSitesGridItemView view = (TopSitesGridItemView) bindView;
 
             // If there is no url, then show "add bookmark".