]> git.ktnx.net Git - mobile-ledger.git/commitdiff
implement stable IDs for the transaction list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 22 Apr 2021 04:39:09 +0000 (04:39 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 22 Apr 2021 04:39:09 +0000 (04:39 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java

index a024b193e403d2a7bb95da1daac5c660af58e66c..6ba456416c42e02c9838c9a89b9c5bd7b24d9e9e 100644 (file)
@@ -41,6 +41,8 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
     public TransactionListAdapter() {
         super();
 
+        setHasStableIds(true);
+
         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
             @Override
             public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
@@ -88,6 +90,24 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
         });
     }
     @Override
+    public long getItemId(int position) {
+        TransactionListItem item = listDiffer.getCurrentList()
+                                             .get(position);
+        switch (item.getType()) {
+            case HEADER:
+                return -1;
+            case TRANSACTION:
+                return item.getTransaction()
+                           .getLedgerId();
+            case DELIMITER:
+                return -item.getDate()
+                            .toDate()
+                            .getTime();
+            default:
+                throw new IllegalStateException("Unexpected value: " + item.getType());
+        }
+    }
+    @Override
     public int getItemViewType(int position) {
         return listDiffer.getCurrentList()
                          .get(position)