File tree Expand file tree Collapse file tree 6 files changed +76
-17
lines changed
src/main/java/com/cuttingedge/PokeApp/Backpack
src/main/java/com/cuttingedge/adapter2recycler/Adapter Expand file tree Collapse file tree 6 files changed +76
-17
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ Add this library as dependency to your project:
5151
5252``` groovy
5353dependencies {
54- compile 'com.github.RobbeSneyders:Modular2Recycler:v1.0.1 '
54+ compile 'com.github.RobbeSneyders:Modular2Recycler:v1.0.2 '
5555}
5656```
5757
Original file line number Diff line number Diff line change 11apply plugin : ' com.android.application'
22
33android {
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 {
2222dependencies {
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}
Original file line number Diff line number Diff line change 44import android .os .Bundle ;
55import android .support .design .widget .FloatingActionButton ;
66import android .support .v4 .content .res .ResourcesCompat ;
7+ import android .support .v7 .util .DiffUtil ;
78import android .support .v7 .widget .LinearLayoutManager ;
89import android .support .v7 .widget .RecyclerView ;
910import 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11apply plugin : ' com.android.library'
22
33android {
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 {
2121dependencies {
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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments