Bug 1270068 - TopSitesGridAdapter: Implement getItemId() so that Android can recycle views appropriately. r=grisha
MozReview-Commit-ID: 5mwPOTcIRRs
--- 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".