Skip to content

Commit ce93f54

Browse files
committed
Fix #7. Add DiffUtil compatibility.
1 parent 2c76334 commit ce93f54

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Add this library as dependency to your project:
5151

5252
```groovy
5353
dependencies {
54-
compile 'com.github.RobbeSneyders:Modular2Recycler:v1.0.1'
54+
compile 'com.github.RobbeSneyders:Modular2Recycler:v1.0.2'
5555
}
5656
```
5757

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.1"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
66

77
defaultConfig {
88
applicationId "com.cuttingedge.pokeapp"
99
minSdkVersion 11
10-
targetSdkVersion 24
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -22,8 +22,8 @@ android {
2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
2424
testCompile 'junit:junit:4.12'
25-
compile 'com.android.support:support-v4:24.1.1'
26-
compile 'com.android.support:appcompat-v7:24.1.1'
27-
compile 'com.android.support:design:24.1.1'
25+
compile 'com.android.support:support-v4:25.1.1'
26+
compile 'com.android.support:appcompat-v7:25.1.1'
27+
compile 'com.android.support:design:25.1.1'
2828
compile project(path: ':library')
2929
}

app/src/main/java/com/cuttingedge/PokeApp/Backpack/BackpackActivity.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.os.Bundle;
55
import android.support.design.widget.FloatingActionButton;
66
import android.support.v4.content.res.ResourcesCompat;
7+
import android.support.v7.util.DiffUtil;
78
import android.support.v7.widget.LinearLayoutManager;
89
import android.support.v7.widget.RecyclerView;
910
import android.support.v7.widget.RecyclerView.ViewHolder;
@@ -86,9 +87,11 @@ private void toggleAlphabetic() {
8687
isAlphabetic = true;
8788
}
8889

89-
List<ModularItem> list = addHeaders(pokemonList);
90-
// swaps the data of the adapter
91-
adapter.swap(list);
90+
List<ModularItem> newList = addHeaders(pokemonList);
91+
List<ModularItem> oldList = adapter.getList();
92+
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffCallback(oldList, newList));
93+
adapter.swap(newList, false);
94+
result.dispatchUpdatesTo(adapter);
9295
}
9396

9497

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.cuttingedge.PokeApp.Backpack;
2+
3+
import android.support.v7.util.DiffUtil;
4+
5+
import com.cuttingedge.adapter2recycler.ModularItem;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Created by Robbe Sneyders.
11+
*
12+
* Class that calculates the difference between two lists.
13+
* Demonstrate the use of DiffUtil
14+
*/
15+
public class DiffCallback extends DiffUtil.Callback {
16+
private List<ModularItem> mOldList;
17+
private List<ModularItem> mNewList;
18+
19+
public DiffCallback(List<ModularItem> oldList, List<ModularItem> newList) {
20+
this.mOldList = oldList;
21+
this.mNewList = newList;
22+
}
23+
24+
@Override
25+
public int getOldListSize() {
26+
return mOldList != null ? mOldList.size() : 0;
27+
}
28+
29+
@Override
30+
public int getNewListSize() {
31+
return mNewList != null ? mNewList.size() : 0;
32+
}
33+
34+
@Override
35+
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
36+
return mNewList.get(newItemPosition) == mOldList.get(oldItemPosition);
37+
}
38+
39+
@Override
40+
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
41+
return mNewList.get(newItemPosition).equals(mOldList.get(oldItemPosition));
42+
}
43+
}

library/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.1"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
66

77
defaultConfig {
88
minSdkVersion 11
9-
targetSdkVersion 24
9+
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
1212
}
@@ -21,7 +21,7 @@ android {
2121
dependencies {
2222
compile fileTree(dir: 'libs', include: ['*.jar'])
2323
testCompile 'junit:junit:4.12'
24-
compile 'com.android.support:support-v4:24.1.1'
25-
compile 'com.android.support:appcompat-v7:24.1.1'
26-
compile 'com.android.support:design:24.1.1'
24+
compile 'com.android.support:support-v4:25.1.1'
25+
compile 'com.android.support:appcompat-v7:25.1.1'
26+
compile 'com.android.support:design:25.1.1'
2727
}

library/src/main/java/com/cuttingedge/adapter2recycler/Adapter/ModularAdapter.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,30 @@ public void removeItems(List<I> items) {
103103

104104
/**
105105
* Swap whole list at once
106+
*
106107
* @param newList new list
107108
*/
108109
public void swap(List<I> newList){
110+
swap(newList, true);
111+
}
112+
113+
/**
114+
* Swap whole list at once
115+
*
116+
* @param newList new list
117+
* @param notify true if method should notify adapter of changes, false otherwise (for example
118+
* when using DiffUtils)
119+
*/
120+
public void swap(List<I> newList, boolean notify){
109121
if (list != null) {
110122
list.clear();
111123
list.addAll(newList);
112124
}
113125
else {
114126
list = newList;
115127
}
116-
notifyDataSetChanged();
128+
if (notify)
129+
notifyDataSetChanged();
117130
}
118131

119132

0 commit comments

Comments
 (0)