1
1
package com .catherine .materialdesignapp .adapters ;
2
2
3
- import android .content .Context ;
4
3
import android .net .Uri ;
5
4
import android .text .TextUtils ;
6
5
import android .view .LayoutInflater ;
7
6
import android .view .MotionEvent ;
8
7
import android .view .View ;
9
8
import android .view .ViewGroup ;
10
9
import android .widget .TextView ;
10
+
11
11
import androidx .annotation .NonNull ;
12
12
import androidx .annotation .Nullable ;
13
13
import androidx .constraintlayout .widget .ConstraintLayout ;
14
+ import androidx .paging .PagedListAdapter ;
14
15
import androidx .recyclerview .selection .ItemDetailsLookup ;
15
16
import androidx .recyclerview .selection .SelectionTracker ;
17
+ import androidx .recyclerview .widget .DiffUtil ;
16
18
import androidx .recyclerview .widget .RecyclerView ;
19
+
17
20
import com .catherine .materialdesignapp .R ;
18
21
import com .catherine .materialdesignapp .jetpack .entities .Artist ;
19
22
import com .catherine .materialdesignapp .utils .DisplayHelper ;
20
23
import com .facebook .drawee .view .SimpleDraweeView ;
21
24
22
- import java .util .ArrayList ;
23
- import java .util .List ;
24
-
25
- public class ArtistAdapter extends RecyclerView .Adapter <ArtistAdapter .MainRvHolder > {
25
+ public class ArtistAdapter extends PagedListAdapter <Artist , ArtistAdapter .MainRvHolder > {
26
26
private final String TAG = ArtistAdapter .class .getSimpleName ();
27
- private Context ctx ;
28
- private List <Artist > entities ;
29
- // private OnItemClickListener listener;
30
-
31
27
public final static int MAX_COLUMNS = 4 ;
32
28
private SelectionTracker <String > selectionTracker ;
33
29
private float cellWidth ;
34
30
35
- public ArtistAdapter (Context ctx , List <Artist > entities /*, OnItemClickListener listener*/ ) {
36
- this .ctx = ctx ;
37
- if (entities == null )
38
- this .entities = new ArrayList <>();
39
- else
40
- this .entities = entities ;
41
- // this.listener = listener;
42
-
43
-
44
- float gridMargin = ctx .getResources ().getDimension (R .dimen .grid_margin );
45
- cellWidth = (DisplayHelper .getScreenWidth () * 1.0f - (MAX_COLUMNS + 1 ) * gridMargin ) / MAX_COLUMNS ;
31
+ public ArtistAdapter () {
32
+ super (DIFF_CALLBACK );
46
33
}
47
34
48
35
public void setSelectionTracker (SelectionTracker <String > selectionTracker ) {
@@ -52,85 +39,49 @@ public void setSelectionTracker(SelectionTracker<String> selectionTracker) {
52
39
@ NonNull
53
40
@ Override
54
41
public MainRvHolder onCreateViewHolder (@ NonNull ViewGroup viewGroup , int i ) {
55
- return new MainRvHolder (LayoutInflater .from (ctx ).inflate (R .layout .rv_artist_item , viewGroup , false ));
42
+ float gridMargin = viewGroup .getContext ().getResources ().getDimension (R .dimen .grid_margin );
43
+ cellWidth = (DisplayHelper .getScreenWidth () * 1.0f - (MAX_COLUMNS + 1 ) * gridMargin ) / MAX_COLUMNS ;
44
+ return new MainRvHolder (LayoutInflater .from (viewGroup .getContext ()).inflate (R .layout .rv_artist_item , viewGroup , false ));
56
45
}
57
46
58
47
@ Override
59
48
public void onBindViewHolder (@ NonNull final MainRvHolder mainRvHolder , final int position ) {
60
- if (entities == null || entities .size () == 0 )
61
- return ;
62
- Artist artist = entities .get (position );
63
-
64
- // OPTION1: setOncClickListener
65
- // if (listener != null) {
66
- // mainRvHolder.itemView.setOnClickListener(v -> listener.onItemClick(mainRvHolder.itemView, position));
67
- // mainRvHolder.itemView.setOnLongClickListener(v -> {
68
- // listener.onItemLongClick(mainRvHolder.itemView, position);
69
- // return false;
70
- // });
71
- // }
72
-
73
- // OPTION2: SelectionTracker
74
- boolean isSelected = false ;
75
- if (selectionTracker != null ) {
76
- if (selectionTracker .isSelected (artist .getArtist ())) {
77
- isSelected = true ;
78
- }
79
- }
80
- mainRvHolder .bind (position , isSelected , artist );
81
-
82
- RecyclerView .LayoutParams params = (RecyclerView .LayoutParams ) mainRvHolder .container .getLayoutParams ();
83
- params .width = Math .round (cellWidth );
84
- params .height = Math .round (cellWidth );
85
- mainRvHolder .container .setLayoutParams (params );
86
-
87
- if (!TextUtils .isEmpty (artist .getImage ())) {
88
- mainRvHolder .sdv_photo .setVisibility (View .VISIBLE );
89
- Uri uri = Uri .parse (artist .getImage ());
90
- // show raw images
91
- mainRvHolder .sdv_photo .setImageURI (uri );
92
- } else
93
- mainRvHolder .sdv_photo .setVisibility (View .GONE );
94
-
95
- if (!TextUtils .isEmpty (artist .getArtist ())) {
96
- mainRvHolder .tv_title .setVisibility (View .VISIBLE );
97
- mainRvHolder .tv_title .setText (artist .getArtist ());
98
- } else
99
- mainRvHolder .tv_title .setVisibility (View .GONE );
100
- }
101
-
102
- @ Override
103
- public int getItemCount () {
104
- return entities .size ();
49
+ Artist artist = getItem (position );
50
+ if (artist != null )
51
+ mainRvHolder .bindTo (position , artist );
52
+ else
53
+ mainRvHolder .clear ();
105
54
}
106
55
107
- @ Override
108
- public long getItemId (int position ) {
109
- return position ;
110
- }
111
-
112
- public void setEntities (List <Artist > entities ) {
113
- this .entities = entities ;
114
- }
115
56
116
57
public class MainRvHolder extends RecyclerView .ViewHolder {
117
- TextView tv_title ;
118
- SimpleDraweeView sdv_photo ;
119
- ConstraintLayout container ;
120
- View itemView ;
121
- ArtistItemDetails artistItemDetails ;
58
+ private TextView tv_title ;
59
+ private SimpleDraweeView sdv_photo ;
60
+ private ConstraintLayout container ;
61
+ private ArtistItemDetails artistItemDetails ;
62
+ private Artist artist ;
122
63
123
64
MainRvHolder (View itemView ) {
124
65
super (itemView );
125
- this .itemView = itemView ;
126
66
container = itemView .findViewById (R .id .container );
127
67
tv_title = itemView .findViewById (R .id .tv_title );
128
68
sdv_photo = itemView .findViewById (R .id .sdv_photo );
129
-
130
69
artistItemDetails = new ArtistItemDetails ();
131
70
}
132
71
133
- void bind (int position , boolean isSelected , Artist artist ) {
72
+ public Artist getArtist () {
73
+ return artist ;
74
+ }
75
+
76
+ void bindTo (int position , Artist artist ) {
77
+ this .artist = artist ;
78
+ boolean isSelected = false ;
79
+ if (selectionTracker != null ) {
80
+ if (selectionTracker .isSelected (artist .getArtist ())) {
81
+ isSelected = true ;
82
+ }
83
+ }
84
+
134
85
artistItemDetails .position = position ;
135
86
artistItemDetails .identifier = artist .getArtist ();
136
87
@@ -140,6 +91,30 @@ void bind(int position, boolean isSelected, Artist artist) {
140
91
} else {
141
92
container .setBackgroundResource (0 );
142
93
}
94
+
95
+ RecyclerView .LayoutParams params = (RecyclerView .LayoutParams ) container .getLayoutParams ();
96
+ params .width = Math .round (cellWidth );
97
+ params .height = Math .round (cellWidth );
98
+ container .setLayoutParams (params );
99
+
100
+ if (!TextUtils .isEmpty (artist .getImage ())) {
101
+ sdv_photo .setVisibility (View .VISIBLE );
102
+ Uri uri = Uri .parse (artist .getImage ());
103
+ // show raw images
104
+ sdv_photo .setImageURI (uri );
105
+ } else
106
+ sdv_photo .setVisibility (View .GONE );
107
+
108
+ if (!TextUtils .isEmpty (artist .getArtist ())) {
109
+ tv_title .setVisibility (View .VISIBLE );
110
+ tv_title .setText (artist .getArtist ());
111
+ } else
112
+ tv_title .setVisibility (View .GONE );
113
+ }
114
+
115
+ void clear () {
116
+ tv_title .setText ("" );
117
+ sdv_photo .setImageURI ("" );
143
118
}
144
119
145
120
public ItemDetailsLookup .ItemDetails <String > getArtistItemDetails (@ NonNull MotionEvent motionEvent ) {
@@ -172,4 +147,18 @@ public boolean inDragRegion(@NonNull MotionEvent e) {
172
147
return true ;
173
148
}
174
149
}
150
+
151
+ private static final DiffUtil .ItemCallback <Artist > DIFF_CALLBACK =
152
+ new DiffUtil .ItemCallback <Artist >() {
153
+ @ Override
154
+ public boolean areItemsTheSame (@ NonNull Artist oldItem , @ NonNull Artist newItem ) {
155
+ return oldItem .compareTo (newItem ) == 0 ;
156
+ }
157
+
158
+ @ Override
159
+ public boolean areContentsTheSame (@ NonNull Artist oldItem ,
160
+ @ NonNull Artist newItem ) {
161
+ return oldItem .getImage ().equals (newItem .getImage ());
162
+ }
163
+ };
175
164
}
0 commit comments