From ff47880a8fb01dac347eadf52a1b911d047ea4b9 Mon Sep 17 00:00:00 2001 From: Hussein Aladeen Date: Fri, 21 Jul 2017 13:49:44 +0100 Subject: [PATCH 1/2] chore(project): Update gradle build tools to version 2.3.3 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index cd33928..7d51051 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.1' + classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files From a8a3d779c88ebf6f5937da88dbb4a65cb285f6d6 Mon Sep 17 00:00:00 2001 From: Hussein Aladeen Date: Fri, 21 Jul 2017 13:50:21 +0100 Subject: [PATCH 2/2] fix(ItemsViewAdapter): Fix logic for setting and checking item position --- .../java/com/fueled/reclaim/BaseItem.java | 8 ++--- .../com/fueled/reclaim/BaseViewHolder.java | 9 ----- .../com/fueled/reclaim/ItemsViewAdapter.java | 35 +++++++++++++++---- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/reclaim/src/main/java/com/fueled/reclaim/BaseItem.java b/reclaim/src/main/java/com/fueled/reclaim/BaseItem.java index a8d5a22..29af89f 100644 --- a/reclaim/src/main/java/com/fueled/reclaim/BaseItem.java +++ b/reclaim/src/main/java/com/fueled/reclaim/BaseItem.java @@ -50,9 +50,8 @@ public void setPositionInAdapter(int positionInAdapter) { * * @return the layout id to be used by the item */ - abstract public @LayoutRes - int getLayoutId(); + abstract public int getLayoutId(); /** * Let the caller know the item is the last one in the adapter @@ -96,8 +95,7 @@ public void setIsLastItem(boolean isLastItem) { * * @param viewHolder the view holder to bind to this item */ - public void onBindViewHolder(T3 viewHolder, int positionInAdapter) { - this.positionInAdapter = positionInAdapter; + public void onBindViewHolder(T3 viewHolder) { this.viewHolder = viewHolder; updateItemViews(); } @@ -140,7 +138,7 @@ public T2 getItemHandler() { * @return the view holder currently attached to this item */ public T3 getViewHolder() { - if (viewHolder != null && viewHolder.getItemBoundTo() == getPositionInAdapter()) { + if (viewHolder != null && viewHolder.getAdapterPosition() == getPositionInAdapter()) { // Make sure the view holder is still bound to this item before returning it. return viewHolder; } diff --git a/reclaim/src/main/java/com/fueled/reclaim/BaseViewHolder.java b/reclaim/src/main/java/com/fueled/reclaim/BaseViewHolder.java index d947218..7fdc73e 100644 --- a/reclaim/src/main/java/com/fueled/reclaim/BaseViewHolder.java +++ b/reclaim/src/main/java/com/fueled/reclaim/BaseViewHolder.java @@ -9,17 +9,8 @@ */ public abstract class BaseViewHolder extends RecyclerView.ViewHolder { - private int itemBoundTo; - public BaseViewHolder(View view) { super(view); } - public int getItemBoundTo() { - return itemBoundTo; - } - - public void setItemBoundTo(int itemBoundTo) { - this.itemBoundTo = itemBoundTo; - } } diff --git a/reclaim/src/main/java/com/fueled/reclaim/ItemsViewAdapter.java b/reclaim/src/main/java/com/fueled/reclaim/ItemsViewAdapter.java index da0ee61..ec57678 100644 --- a/reclaim/src/main/java/com/fueled/reclaim/ItemsViewAdapter.java +++ b/reclaim/src/main/java/com/fueled/reclaim/ItemsViewAdapter.java @@ -1,6 +1,7 @@ package com.fueled.reclaim; import android.content.Context; +import android.support.v7.util.DiffUtil; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -36,7 +37,8 @@ public void addItemsList(List items) { int startSize = getItemCount(); this.items.addAll(items); - notifyItemRangeChanged(startSize, items.size()); + updateItemPositions(startSize); + notifyItemRangeInserted(startSize, items.size()); } /** @@ -48,6 +50,7 @@ public void addItemsList(List items) { */ public void addItemsList(int position, List items) { this.items.addAll(position, items); + updateItemPositions(position); notifyItemRangeInserted(position, items.size()); } @@ -59,6 +62,7 @@ public void addItemsList(int position, List items) { public void replaceItems(List items) { this.items.clear(); this.items.addAll(items); + updateItemPositions(0); notifyDataSetChanged(); } @@ -73,13 +77,20 @@ public void replaceItems(List items, boolean useDiffCheck) { if (!useDiffCheck) { replaceItems(items); } else { - DiffChecker.calculateDiff(this.items, items) - .dispatchUpdatesTo(this); + DiffUtil.DiffResult result = DiffChecker.calculateDiff(this.items, items); this.items.clear(); this.items.addAll(items); + updateItemPositions(0); + result.dispatchUpdatesTo(this); } } + public void replaceItemAt(int position, BaseItem item) { + items.set(position, item); + item.setPositionInAdapter(position); + notifyItemChanged(position); + } + /** * Add item to the adapter. * @@ -97,6 +108,7 @@ public void addItem(BaseItem item) { */ public void addItem(int position, BaseItem item) { items.add(position, item); + updateItemPositions(position); notifyItemInserted(position); } @@ -107,6 +119,7 @@ public void addItem(int position, BaseItem item) { */ public void removeItemAt(int position) { items.remove(position); + updateItemPositions(position); notifyItemRemoved(position); } @@ -114,8 +127,17 @@ public void removeItemAt(int position) { * Removes all items inside the recycler adapter view. */ public void clearAllRecyclerItems() { + int total = items.size(); + items.clear(); - notifyDataSetChanged(); + + notifyItemRangeRemoved(0, total); + } + + private void updateItemPositions(int startPosition) { + for (int i = startPosition; i < items.size(); i++) { + items.get(i).setPositionInAdapter(i); + } } /** @@ -145,8 +167,7 @@ public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { @Override @SuppressWarnings("unchecked") public void onBindViewHolder(BaseViewHolder holder, int position) { - holder.setItemBoundTo(position); - items.get(position).onBindViewHolder(holder, position); + items.get(position).onBindViewHolder(holder); } @Override @@ -165,4 +186,4 @@ public EmptyViewHolder(View view) { } } -} +} \ No newline at end of file