Skip to content

Commit 77f0737

Browse files
committed
Add a bit of safety when calling getViewHolderAt
1 parent ca0f2cb commit 77f0737

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

adapterflowlayout/src/main/java/com/commit451/adapterflowlayout/AdapterFlowLayout.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public RecyclerView.Adapter getAdapter() {
4545

4646
@Nullable
4747
public RecyclerView.ViewHolder getViewHolderAt(int index) {
48-
return mAdapterLayoutDelegate.getViewHolderAt(index);
48+
if (mAdapterLayoutDelegate != null) {
49+
return mAdapterLayoutDelegate.getViewHolderAt(index);
50+
}
51+
return null;
4952
}
5053
}

adapterlayout/src/main/java/com/commit451/adapterlayout/AdapterLayoutDelegate.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class AdapterLayoutDelegate {
1616
private ViewGroup mViewGroup;
1717

1818
/**
19-
* Checks for if the data changes and changes the views accordingly
19+
* Checks for if the data changes and changes the views accordingly
2020
*/
2121
private RecyclerView.AdapterDataObserver mObserver = new RecyclerView.AdapterDataObserver() {
2222
@Override
@@ -60,6 +60,7 @@ public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
6060

6161
/**
6262
* Create a new delegate, acting as the bridge between the adapter and the ViewGroup
63+
*
6364
* @param viewGroup the ViewGroup which will have views added and removed from
6465
*/
6566
public AdapterLayoutDelegate(ViewGroup viewGroup) {
@@ -68,13 +69,15 @@ public AdapterLayoutDelegate(ViewGroup viewGroup) {
6869

6970
/**
7071
* Set the adapter which will add and remove views from this layout
72+
*
7173
* @param adapter the adapter
7274
*/
73-
public void setAdapter(RecyclerView.Adapter adapter) {
75+
public void setAdapter(@Nullable RecyclerView.Adapter adapter) {
7476
if (mAdapter != null) {
7577
try {
7678
mAdapter.unregisterAdapterDataObserver(mObserver);
77-
} catch (Exception ignored) {}
79+
} catch (Exception ignored) {
80+
}
7881
}
7982

8083
mAdapter = adapter;
@@ -86,18 +89,22 @@ public void setAdapter(RecyclerView.Adapter adapter) {
8689

8790
/**
8891
* Returns the adapter which was passed via {@link #setAdapter(RecyclerView.Adapter)}
92+
*
8993
* @return the adapter
9094
*/
91-
public @Nullable RecyclerView.Adapter getAdapter() {
95+
@Nullable
96+
public RecyclerView.Adapter getAdapter() {
9297
return mAdapter;
9398
}
9499

95100
/**
96101
* Return the {@link android.support.v7.widget.RecyclerView.ViewHolder} at the specified position.
102+
*
97103
* @param index the position at which to get the ViewHolder
98104
* @return the ViewHolder at the index, or null if none exists
99105
*/
100-
public @Nullable RecyclerView.ViewHolder getViewHolderAt(int index) {
106+
@Nullable
107+
public RecyclerView.ViewHolder getViewHolderAt(int index) {
101108
View view = mViewGroup.getChildAt(index);
102109
if (view == null) {
103110
return null;
@@ -107,7 +114,7 @@ public void setAdapter(RecyclerView.Adapter adapter) {
107114

108115
private void addViews(int positionStart, int itemCount) {
109116
final int end = positionStart + itemCount;
110-
for (int i=positionStart; i<end; i++) {
117+
for (int i = positionStart; i < end; i++) {
111118
addViewAt(i);
112119
}
113120
}
@@ -124,16 +131,11 @@ private void addViewAt(int viewType, int index) {
124131
mAdapter.onBindViewHolder(viewHolder, index);
125132
}
126133

127-
private void updateViews(int positionStart, int itemCount, Object payload) {
128-
//TODO do something with the payload?
134+
private void updateViews(int positionStart, int itemCount, @Nullable Object payload) {
129135
final int end = positionStart + itemCount;
130-
for (int i=positionStart; i<end; i++) {
136+
for (int i = positionStart; i < end; i++) {
131137
RecyclerView.ViewHolder viewHolder = getViewHolderAt(i);
132-
if (payload != null) {
133-
mAdapter.onBindViewHolder(viewHolder, i);
134-
} else {
135-
mAdapter.onBindViewHolder(viewHolder, i);
136-
}
138+
mAdapter.onBindViewHolder(viewHolder, i);
137139
}
138140
}
139141

adapterlayout/src/main/java/com/commit451/adapterlayout/AdapterLinearLayout.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public RecyclerView.Adapter getAdapter() {
5353

5454
@Nullable
5555
public RecyclerView.ViewHolder getViewHolderAt(int index) {
56-
return mAdapterLayoutDelegate.getViewHolderAt(index);
56+
if (mAdapterLayoutDelegate != null) {
57+
return mAdapterLayoutDelegate.getViewHolderAt(index);
58+
}
59+
return null;
5760
}
5861
}

app/src/main/java/com/commit451/adapterlayout/sample/CheeseAdapter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
/**
1111
* Adapter for the recyclerview, which holds cheeses
12-
* Created by John on 11/24/15.
1312
*/
1413
public class CheeseAdapter extends RecyclerView.Adapter<CheeseViewHolder> {
1514

0 commit comments

Comments
 (0)