Skip to content

Commit

Permalink
Fixed the crash on setting divider decoration before setting adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheungon committed Feb 12, 2016
1 parent 6135269 commit 462576d
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public int dividerSize(int position, RecyclerView parent) {

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int itemCount = parent.getAdapter().getItemCount();
RecyclerView.Adapter adapter = parent.getAdapter();
int itemCount = adapter == null ? 0 : adapter.getItemCount();
int lastDividerOffset = getLastDividerOffset(parent);
int validChildCount = parent.getChildCount();
int lastChildPosition = -1;
Expand Down Expand Up @@ -134,8 +135,9 @@ public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

@Override
public void getItemOffsets(Rect rect, View v, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(v);
int itemCount = parent.getAdapter().getItemCount();
RecyclerView.Adapter adapter = parent.getAdapter();
int position = adapter == null ? 0 : parent.getChildAdapterPosition(v);
int itemCount = adapter == null ? 0 : adapter.getItemCount();
int lastDividerOffset = getLastDividerOffset(parent);
if (!mShowLastDivider && position >= itemCount - lastDividerOffset) {
// Don't set item offset for last line if mShowLastDivider = false
Expand All @@ -159,10 +161,11 @@ public void getItemOffsets(Rect rect, View v, RecyclerView parent, RecyclerView.
*/
private int getLastDividerOffset(RecyclerView parent) {
if (parent.getLayoutManager() instanceof GridLayoutManager) {
RecyclerView.Adapter adapter = parent.getAdapter();
GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
int spanCount = layoutManager.getSpanCount();
int itemCount = parent.getAdapter().getItemCount();
int itemCount = adapter == null ? 0 : adapter.getItemCount();
for (int i = itemCount - 1; i >= 0; i--) {
if (spanSizeLookup.getSpanIndex(i, spanCount) == 0) {
return itemCount - i;
Expand Down

0 comments on commit 462576d

Please sign in to comment.