Skip to content

Commit 5b83da4

Browse files
committed
Refactoring.
1 parent 01223fe commit 5b83da4

File tree

2 files changed

+89
-102
lines changed

2 files changed

+89
-102
lines changed

library/src/main/java/de/mrapp/android/dialog/adapter/RecyclerViewAdapterWrapper.java

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import androidx.annotation.NonNull;
2727
import androidx.annotation.Nullable;
2828
import androidx.recyclerview.widget.RecyclerView;
29+
2930
import de.mrapp.android.dialog.R;
3031
import de.mrapp.android.dialog.model.ListDialog.OnItemSelectedListener;
3132
import de.mrapp.android.util.ThemeUtil;
@@ -35,8 +36,7 @@
3536
* A wrapper that encapsulates a recycler view adapter in order to manage the selection states of
3637
* the adapter's list items.
3738
*
38-
* @param <VH>
39-
* The type of the view holder of the encapsulated adapter
39+
* @param <VH> The type of the view holder of the encapsulated adapter
4040
* @author Michael Rapp
4141
* @since 5.0.0
4242
*/
@@ -52,12 +52,10 @@ public interface OnItemClickListener {
5252
/**
5353
* The method, which is invoked, when a list item has been clicked.
5454
*
55-
* @param adapter
56-
* The adapter, the list item belongs to, as an instance of the class {@link
57-
* RecyclerViewAdapterWrapper}. The adapter may not be null
58-
* @param position
59-
* The position of the list item, which has been clicked, as an {@link Integer}
60-
* value
55+
* @param adapter The adapter, the list item belongs to, as an instance of the class {@link
56+
* RecyclerViewAdapterWrapper}. The adapter may not be null
57+
* @param position The position of the list item, which has been clicked, as an {@link Integer}
58+
* value
6159
*/
6260
void onItemClick(@NonNull RecyclerViewAdapterWrapper<?> adapter, int position);
6361

@@ -72,21 +70,18 @@ public interface ChoiceMode {
7270
/**
7371
* Returns, whether the list item at a specific position is currently selected, or not.
7472
*
75-
* @param position
76-
* The position of the list item, whose selection state should be returned, as an
77-
* {@link Integer} value
73+
* @param position The position of the list item, whose selection state should be returned, as an
74+
* {@link Integer} value
7875
* @return True, if the list item is selected, false otherwise
7976
*/
8077
boolean isItemChecked(int position);
8178

8279
/**
8380
* Sets whether the list item at a specific position should be selected, or not.
8481
*
85-
* @param position
86-
* The position of the list item, whose selection state should be changed, as an
87-
* {@link Integer} value
88-
* @param checked
89-
* True, if the list item should be selected, false otherwise
82+
* @param position The position of the list item, whose selection state should be changed, as an
83+
* {@link Integer} value
84+
* @param checked True, if the list item should be selected, false otherwise
9085
* @return True, if the selection of the list item has been changed, false otherwise
9186
*/
9287
boolean setItemChecked(int position, boolean checked);
@@ -101,7 +96,16 @@ public static class SingleChoiceMode implements ChoiceMode {
10196
/**
10297
* The position of the currently selected list item.
10398
*/
104-
private int checkedItem = 0;
99+
private int checkedItem;
100+
101+
/**
102+
* Creates a new choice mode, which allows only a single list item to be selected at once.
103+
*
104+
* @param checkedItem The position of the list item, which should be selected by default
105+
*/
106+
public SingleChoiceMode(final int checkedItem) {
107+
this.checkedItem = checkedItem;
108+
}
105109

106110
@Override
107111
public final boolean isItemChecked(final int position) {
@@ -130,6 +134,22 @@ public static class MultipleChoiceMode implements ChoiceMode {
130134
*/
131135
private final Set<Integer> checkedItems = new HashSet<>();
132136

137+
/**
138+
* Creates a new choice mode, which allows multiple list items to be selected at once.
139+
*
140+
* @param checkedItems A boolean array, which specifies whether the list items at a specific
141+
* position is selected or not
142+
*/
143+
public MultipleChoiceMode(@Nullable final boolean[] checkedItems) {
144+
if (checkedItems != null) {
145+
for (int i = 0; i < checkedItems.length; i++) {
146+
if (checkedItems[i]) {
147+
this.checkedItems.add(i);
148+
}
149+
}
150+
}
151+
}
152+
133153
@Override
134154
public final boolean isItemChecked(final int position) {
135155
return checkedItems.contains(position);
@@ -166,8 +186,7 @@ public final boolean setItemChecked(final int position, final boolean checked) {
166186
/**
167187
* A wrapper, which encapsulates a view holder.
168188
*
169-
* @param <T>
170-
* The type of the encapsulated view holder
189+
* @param <T> The type of the encapsulated view holder
171190
*/
172191
public static class ViewHolderWrapper<T extends RecyclerView.ViewHolder>
173192
extends RecyclerView.ViewHolder {
@@ -180,12 +199,10 @@ public static class ViewHolderWrapper<T extends RecyclerView.ViewHolder>
180199
/**
181200
* Creates a new wrapper, which encapsulates a specific view holder.
182201
*
183-
* @param itemView
184-
* The view, the wrapper corresponds to, as an instance of the class {@link View}.
185-
* The view may not be null
186-
* @param wrappedViewHolder
187-
* The encapsulated view holder as an instance of the generic type T. The view
188-
* holder may not be null
202+
* @param itemView The view, the wrapper corresponds to, as an instance of the class {@link View}.
203+
* The view may not be null
204+
* @param wrappedViewHolder The encapsulated view holder as an instance of the generic type T. The view
205+
* holder may not be null
189206
*/
190207
public ViewHolderWrapper(@NonNull final View itemView, @NonNull final T wrappedViewHolder) {
191208
super(itemView);
@@ -234,8 +251,7 @@ public T getWrappedViewHolder() {
234251
/**
235252
* Creates a listener, which allows to select a list item when it has been clicked.
236253
*
237-
* @param position
238-
* The position of the list item as an {@link Integer} value
254+
* @param position The position of the list item as an {@link Integer} value
239255
* @return The listener, which has been created, as an instance of the type {@link
240256
* View.OnClickListener}. The listener may not be null
241257
*/
@@ -259,11 +275,9 @@ public void onClick(View v) {
259275
* Creates and returns a runnable, which allows to change the selection state of a specific
260276
* {@link Checkable}.
261277
*
262-
* @param checkable
263-
* The checkable, whose selection state should be changed, as an instance of the type
264-
* {@link Checkable}. The checkable may not be null.
265-
* @param checked
266-
* True, if the checkable should be selected, false otherwise
278+
* @param checkable The checkable, whose selection state should be changed, as an instance of the type
279+
* {@link Checkable}. The checkable may not be null.
280+
* @param checked True, if the checkable should be selected, false otherwise
267281
* @return The runnable, which has been created, as an instance of the type {@link Runnable}.
268282
* The runnable may not be null
269283
*/
@@ -284,15 +298,12 @@ public void run() {
284298
* Creates a new wrapper that encapsulates a recycler view adapter in order to manage the
285299
* selection states of the adapter's list items.
286300
*
287-
* @param context
288-
* The context, which should be used by the wrapper, as an instance of the class {@link
289-
* Context}. The context may not be null
290-
* @param wrappedAdapter
291-
* The encapsulated recycler view adapter as an instance of the class
292-
* RecyclerView.Adapter. The adapter may not be null
293-
* @param choiceMode
294-
* The choice mode, which should be used by the adapter, as an instance of the type
295-
* {@link ChoiceMode}. The choice mode may not be null
301+
* @param context The context, which should be used by the wrapper, as an instance of the class {@link
302+
* Context}. The context may not be null
303+
* @param wrappedAdapter The encapsulated recycler view adapter as an instance of the class
304+
* RecyclerView.Adapter. The adapter may not be null
305+
* @param choiceMode The choice mode, which should be used by the adapter, as an instance of the type
306+
* {@link ChoiceMode}. The choice mode may not be null
296307
*/
297308
public RecyclerViewAdapterWrapper(@NonNull final Context context,
298309
@NonNull final RecyclerView.Adapter<VH> wrappedAdapter,
@@ -308,9 +319,8 @@ public RecyclerViewAdapterWrapper(@NonNull final Context context,
308319
/**
309320
* Sets the listener, which should be notified, when a list item has been clicked.
310321
*
311-
* @param listener
312-
* The listener, which should be set, as an instance of the type {@link
313-
* OnItemClickListener} or null, if no listener should be notified
322+
* @param listener The listener, which should be set, as an instance of the type {@link
323+
* OnItemClickListener} or null, if no listener should be notified
314324
*/
315325
public final void setOnItemClickListener(@Nullable final OnItemClickListener listener) {
316326
this.itemClickListener = listener;
@@ -319,9 +329,8 @@ public final void setOnItemClickListener(@Nullable final OnItemClickListener lis
319329
/**
320330
* Sets the listener, which should be notified, when a list item has been selected.
321331
*
322-
* @param listener
323-
* The listener, which should be set, as an instance of the type {@link
324-
* OnItemSelectedListener} or null, if no listener should be notified
332+
* @param listener The listener, which should be set, as an instance of the type {@link
333+
* OnItemSelectedListener} or null, if no listener should be notified
325334
*/
326335
public final void setOnItemSelectedListener(@Nullable final OnItemSelectedListener listener) {
327336
this.itemSelectedListener = listener;
@@ -352,9 +361,8 @@ public final RecyclerView.Adapter<VH> getWrappedAdapter() {
352361
/**
353362
* Returns, whether the list item at a specific position is currently selected, or not.
354363
*
355-
* @param position
356-
* The position of the list item, whose selection state should be returned, as an {@link
357-
* Integer} value
364+
* @param position The position of the list item, whose selection state should be returned, as an {@link
365+
* Integer} value
358366
* @return True, if the list item is selected, false otherwise
359367
*/
360368
public final boolean isItemChecked(final int position) {
@@ -364,11 +372,9 @@ public final boolean isItemChecked(final int position) {
364372
/**
365373
* Sets, whether the list item at a specific position should be selected, or not.
366374
*
367-
* @param position
368-
* The position of the list item, whose selection state should be changed, as an {@link
369-
* Integer} value
370-
* @param checked
371-
* True, if the list item should be selected, false otherwise
375+
* @param position The position of the list item, whose selection state should be changed, as an {@link
376+
* Integer} value
377+
* @param checked True, if the list item should be selected, false otherwise
372378
*/
373379
public final void setItemChecked(final int position, final boolean checked) {
374380
if (choiceMode.setItemChecked(position, checked)) {
@@ -402,9 +408,10 @@ public final void onBindViewHolder(@NonNull final ViewHolderWrapper<VH> holder,
402408
wrappedAdapter.onBindViewHolder(wrappedViewHolder, position);
403409
View view = holder.itemView;
404410
view.setOnClickListener(createItemClickListener(position));
411+
View wrappedView = wrappedViewHolder.itemView;
405412

406-
if (view instanceof Checkable) {
407-
handler.post(createCheckableRunnable((Checkable) view, isItemChecked(position)));
413+
if (wrappedView instanceof Checkable) {
414+
handler.post(createCheckableRunnable((Checkable) wrappedView, isItemChecked(position)));
408415
}
409416
}
410417

0 commit comments

Comments
 (0)