diff --git a/example/src/main/java/com/github/vivchar/example/pages/github/GithubPresenter.java b/example/src/main/java/com/github/vivchar/example/pages/github/GithubPresenter.java index fc548f9..7822433 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/github/GithubPresenter.java +++ b/example/src/main/java/com/github/vivchar/example/pages/github/GithubPresenter.java @@ -106,6 +106,7 @@ public void viewShown() { * * I don't change the first item position because here is the bug * https://stackoverflow.com/a/43461324/4894238 + * use RendererRecyclerViewAdapter.setUpdateCallback(ListUpdateCallback) if you want */ Log.d(TAG, "topStargazersModels: " + topStargazersModels.size()); diff --git a/example/src/main/java/com/github/vivchar/example/pages/simple/YourDataProvider.java b/example/src/main/java/com/github/vivchar/example/pages/simple/YourDataProvider.java index 5a6e609..5346341 100644 --- a/example/src/main/java/com/github/vivchar/example/pages/simple/YourDataProvider.java +++ b/example/src/main/java/com/github/vivchar/example/pages/simple/YourDataProvider.java @@ -1,8 +1,11 @@ package com.github.vivchar.example.pages.simple; +import android.support.v7.util.ListUpdateCallback; + import com.github.vivchar.example.pages.simple.DiffUtilFragment.DiffViewModel; import com.github.vivchar.example.pages.simple.PayloadFragment.PayloadViewModel; import com.github.vivchar.rendererrecyclerviewadapter.DefaultCompositeViewModel; +import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter; import com.github.vivchar.rendererrecyclerviewadapter.ViewModel; import java.util.ArrayList; @@ -39,7 +42,11 @@ public List getUpdatedDiffItems(final DiffViewModel model) { final ViewModel clickedModel = mDiffItems.remove(clickedIndex); final ViewModel remove = mDiffItems.remove(0); Collections.shuffle(mDiffItems); - mDiffItems.add(0, remove); /* https://stackoverflow.com/a/43461324/4894238 */ + + /* https://stackoverflow.com/a/43461324/4894238 + * use RendererRecyclerViewAdapter.setUpdateCallback(ListUpdateCallback) if you want + * */ + mDiffItems.add(0, remove); mDiffItems.add(clickedIndex, clickedModel); return mDiffItems; } diff --git a/gradle.properties b/gradle.properties index 5d011df..ddb0716 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,8 +15,8 @@ org.gradle.jvmargs=-Xmx1536m # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -VERSION_NAME=2.3.0 -VERSION_CODE=23 +VERSION_NAME=2.3.1 +VERSION_CODE=24 GROUP=com.github.vivchar POM_DESCRIPTION=A single adapter with multiple view types for the whole project diff --git a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/RendererRecyclerViewAdapter.java b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/RendererRecyclerViewAdapter.java index 99fdbf0..b1ad19d 100644 --- a/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/RendererRecyclerViewAdapter.java +++ b/rendererrecyclerviewadapter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/RendererRecyclerViewAdapter.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.util.DiffUtil; +import android.support.v7.util.ListUpdateCallback; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.ViewGroup; @@ -31,6 +32,8 @@ public class RendererRecyclerViewAdapter extends RecyclerView.Adapter mBoundViewHolders = new ArrayList<>(); + @Nullable + private ListUpdateCallback mUpdateCallback = null; @NonNull protected DiffCallback mDiffCallback = new DefaultDiffCallback(); @NonNull @@ -170,6 +173,10 @@ public void setDiffCallback(@NonNull final DiffCallback diffCallback) { enableDiffUtil(); } + public void setUpdateCallback(@NonNull final ListUpdateCallback updateCallback) { + mUpdateCallback = updateCallback; + } + public void setItems(@NonNull final List items) { if (mDiffUtilEnabled) { mDiffCallback.setItems(mItems, items); @@ -179,7 +186,39 @@ public void setItems(@NonNull final List items) { mItems.clear(); mItems.addAll(items); - diffResult.dispatchUpdatesTo(this); + diffResult.dispatchUpdatesTo(new ListUpdateCallback() { + @Override + public void onInserted(final int position, final int count) { + if (mUpdateCallback != null) { + mUpdateCallback.onInserted(position, count); + } + notifyItemRangeInserted(position, count); + } + + @Override + public void onRemoved(final int position, final int count) { + if (mUpdateCallback != null) { + mUpdateCallback.onRemoved(position, count); + } + notifyItemRangeRemoved(position, count); + } + + @Override + public void onMoved(final int fromPosition, final int toPosition) { + if (mUpdateCallback != null) { + mUpdateCallback.onMoved(fromPosition, toPosition); + } + notifyItemMoved(fromPosition, toPosition); + } + + @Override + public void onChanged(final int position, final int count, final Object payload) { + if (mUpdateCallback != null) { + mUpdateCallback.onChanged(position, count, payload); + } + notifyItemRangeChanged(position, count, payload); + } + }); } else { mItems.clear(); mItems.addAll(items);