Skip to content

Commit

Permalink
added ListUpdateCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
vivchar committed Jan 2, 2018
1 parent 3f3581a commit 9608f16
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,7 +42,11 @@ public List<ViewModel> 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;
}
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,6 +32,8 @@ public class RendererRecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder
@NonNull
protected final ArrayList<ViewHolder> mBoundViewHolders = new ArrayList<>();

@Nullable
private ListUpdateCallback mUpdateCallback = null;
@NonNull
protected DiffCallback mDiffCallback = new DefaultDiffCallback();
@NonNull
Expand Down Expand Up @@ -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<? extends ViewModel> items) {
if (mDiffUtilEnabled) {
mDiffCallback.setItems(mItems, items);
Expand All @@ -179,7 +186,39 @@ public void setItems(@NonNull final List<? extends ViewModel> 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);
Expand Down

0 comments on commit 9608f16

Please sign in to comment.