From 93a2dba5b0a6957564dd4375a7a3b9a14624500e Mon Sep 17 00:00:00 2001 From: pedrovgs Date: Mon, 1 May 2017 14:15:04 +0200 Subject: [PATCH] Clean diff feature before to publish the new release --- README.md | 10 +++ .../pedrogomez/renderers/DiffCallbacks.java | 68 +++++++++++++++++ .../renderers/RVRendererAdapter.java | 75 ++++--------------- .../sample/ui/RecyclerViewActivity.java | 33 ++------ 4 files changed, 99 insertions(+), 87 deletions(-) create mode 100644 renderers/src/main/java/com/pedrogomez/renderers/DiffCallbacks.java diff --git a/README.md b/README.md index 0eb7459..0d5eae2 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,16 @@ private void initListView() { **Remember if you are going to use ``RecyclerView`` instead of ``ListView`` you'll have to use ``RVRendererAdapter`` instead of ``RendererAdapter``.** +* 4. Diff updates. + +If the ``RecyclerView`` performance is crucial in your application remember you can use ``diffUpdate`` method in your ``RVRendererAdapter`` instance to update just the items changed in your adapter and not the whole list. + +```java +adapter.diffUpdate(newList) +``` + +This method provides a ready to use diff update for our adapter based on the implementation of the standard ``equals`` method from ``Object`` class. + Usage ----- diff --git a/renderers/src/main/java/com/pedrogomez/renderers/DiffCallbacks.java b/renderers/src/main/java/com/pedrogomez/renderers/DiffCallbacks.java new file mode 100644 index 0000000..7ef716a --- /dev/null +++ b/renderers/src/main/java/com/pedrogomez/renderers/DiffCallbacks.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.pedrogomez.renderers; + +import android.support.v7.util.DiffUtil; + +import java.util.List; + +class DiffCallbacks extends DiffUtil.Callback { + + private final AdapteeCollection oldList; + private final List newList; + + private int oldItemPosition; + + private boolean deep; + + DiffCallbacks(AdapteeCollection oldList, List newList) { + this.oldList = oldList; + this.newList = newList; + } + + @Override public int getOldListSize() { + return oldList.size(); + } + + @Override public int getNewListSize() { + return newList.size(); + } + + @Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { + this.deep = false; + this.oldItemPosition = oldItemPosition; + return equals(newList.get(newItemPosition)); + } + + @Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { + this.deep = true; + this.oldItemPosition = oldItemPosition; + return equals(newList.get(newItemPosition)); + } + + @Override public boolean equals(Object newItem) { + Object current = oldList.get(oldItemPosition); + if (deep) { + return newItem.equals(current); + } else { + return newItem.getClass().equals(current.getClass()); + } + } + + @Override public int hashCode() { + return super.hashCode(); + } +} \ No newline at end of file diff --git a/renderers/src/main/java/com/pedrogomez/renderers/RVRendererAdapter.java b/renderers/src/main/java/com/pedrogomez/renderers/RVRendererAdapter.java index cdbe68b..c00275c 100644 --- a/renderers/src/main/java/com/pedrogomez/renderers/RVRendererAdapter.java +++ b/renderers/src/main/java/com/pedrogomez/renderers/RVRendererAdapter.java @@ -19,17 +19,19 @@ import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.ViewGroup; + import com.pedrogomez.renderers.exception.NullRendererBuiltException; + import java.util.Collection; import java.util.List; /** * RecyclerView.Adapter extension created to work RendererBuilders and Renderer instances. Other * adapters have to use this one to show information into RecyclerView widgets. - * + *

* This class is the heart of this library. It's used to avoid the library users declare a new * renderer each time they have to show information into a RecyclerView. - * + *

* RVRendererAdapter has to be constructed with a LayoutInflater to inflate views, one * RendererBuilder to provide Renderer to RVRendererAdapter and one AdapteeCollection to * provide the elements to render. @@ -63,14 +65,14 @@ public T getItem(int position) { } public void setCollection(AdapteeCollection collection) { - if (collection == null) { - throw new IllegalArgumentException("The AdapteeCollection configured can't be null"); - } + if (collection == null) { + throw new IllegalArgumentException("The AdapteeCollection configured can't be null"); + } - this.collection = collection; + this.collection = collection; } - /** + /** * Indicate to the RecyclerView the type of Renderer used to one position using a numeric value. * * @param position to analyze. @@ -178,7 +180,7 @@ protected AdapteeCollection getCollection() { /** * Empty implementation created to allow the client code to extend this class without override * getView method. - * + *

* This method is called before render the Renderer and can be used in RendererAdapter extension * to add extra info to the renderer created like the position in the ListView/RecyclerView. * @@ -192,67 +194,20 @@ protected void updateRendererExtraValues(T content, Renderer renderer, int po /** * Provides a ready to use diff update for our adapter based on the implementation of the - * standard equals method from Object + * standard equals method from Object. * * @param newList to refresh our content */ public void diffUpdate(List newList) { if (getCollection().size() == 0) { - this.addAll(newList); + addAll(newList); notifyDataSetChanged(); } else { - DiffCallbacks diffCallbacks = new DiffCallbacks(newList); + DiffCallbacks diffCallbacks = new DiffCallbacks(collection, newList); DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallbacks); - - this.clear(); - this.addAll(newList); + clear(); + addAll(newList); diffResult.dispatchUpdatesTo(this); } } - - private class DiffCallbacks extends DiffUtil.Callback { - - private final List newList; - - private int oldItemPosition; - - private boolean deep; - - DiffCallbacks(List newList) { - this.newList = newList; - } - - @Override public int getOldListSize() { - return collection.size(); - } - - @Override public int getNewListSize() { - return newList.size(); - } - - @Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { - this.deep = false; - this.oldItemPosition = oldItemPosition; - return equals(newList.get(newItemPosition)); - } - - @Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { - this.deep = true; - this.oldItemPosition = oldItemPosition; - return equals(newList.get(newItemPosition)); - } - - @Override public boolean equals(Object newItem) { - Object current = collection.get(oldItemPosition); - if (deep) { - return newItem.equals(current); - } else { - return newItem.getClass().equals(current.getClass()); - } - } - - @Override public int hashCode() { - return super.hashCode(); - } - } } diff --git a/sample/src/main/java/com/pedrogomez/renderers/sample/ui/RecyclerViewActivity.java b/sample/src/main/java/com/pedrogomez/renderers/sample/ui/RecyclerViewActivity.java index b1a17d0..a31e517 100644 --- a/sample/src/main/java/com/pedrogomez/renderers/sample/ui/RecyclerViewActivity.java +++ b/sample/src/main/java/com/pedrogomez/renderers/sample/ui/RecyclerViewActivity.java @@ -64,17 +64,17 @@ private void initAdapter() { new RandomVideoCollectionGenerator(); final AdapteeCollection