Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix for issue #18
Browse files Browse the repository at this point in the history
  • Loading branch information
ShamylZakariya committed Jul 29, 2016
1 parent a7fb872 commit 4b9dc86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zakariya.stickyheadersapp.ui;

import android.os.Bundle;
import android.os.Handler;

import org.zakariya.stickyheaders.StickyHeaderLayoutManager;
import org.zakariya.stickyheadersapp.adapters.SimpleDemoAdapter;
Expand All @@ -13,6 +14,17 @@ public class StressTestDemoActivity extends DemoActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView.setLayoutManager(new StickyHeaderLayoutManager());
recyclerView.setAdapter(new SimpleDemoAdapter(1000, 5, true, false, false, SHOW_ADAPTER_POSITIONS));

// this is a really crude emulation of a scenario where the layout manager
// has been assigned, but the adapter isn't created and assigned until data is
// made available (say, because of network latency).

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.setAdapter(new SimpleDemoAdapter(1000, 5, true, false, false, SHOW_ADAPTER_POSITIONS));
}
}, 500);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,38 @@ public void setHeaderPositionChangedCallback(HeaderPositionChangedCallback heade
}

@Override
public void onAttachedToWindow(RecyclerView view) {
super.onAttachedToWindow(view);
public void onAdapterChanged(RecyclerView.Adapter oldAdapter, RecyclerView.Adapter newAdapter) {
super.onAdapterChanged(oldAdapter, newAdapter);

try {
adapter = (SectioningAdapter) view.getAdapter();
adapter = (SectioningAdapter) newAdapter;
} catch (ClassCastException e) {
throw new ClassCastException("StickyHeaderLayoutManager must be used with a RecyclerView where the adapter is a kind of SectioningAdapter");
}
}

@Override
public void onAdapterChanged(RecyclerView.Adapter oldAdapter, RecyclerView.Adapter newAdapter) {
removeAllViews();
headerViews.clear();
headerPositionsBySection.clear();
}

@Override
public void onAttachedToWindow(RecyclerView view) {
super.onAttachedToWindow(view);

try {
adapter = (SectioningAdapter) view.getAdapter();
} catch (ClassCastException e) {
throw new ClassCastException("StickyHeaderLayoutManager must be used with a RecyclerView where the adapter is a kind of SectioningAdapter");
}
}

@Override
public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) {
super.onDetachedFromWindow(view, recycler);

// Update positions in case we need to save post-detach
updateFirstAdapterPosition();
adapter = null;
//adapter = null;
}

@Override
Expand Down Expand Up @@ -152,6 +161,10 @@ public void onRestoreInstanceState(Parcelable state) {
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

if (adapter == null) {
return;
}

if (scrollTargetAdapterPosition >= 0) {
firstViewAdapterPosition = scrollTargetAdapterPosition;
firstViewTop = 0;
Expand All @@ -178,8 +191,9 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
int totalVendedHeight = 0;

// If we emptied the view with a notify, we may overshoot and fail to draw
if (firstViewAdapterPosition > state.getItemCount())
if (firstViewAdapterPosition > state.getItemCount()) {
firstViewAdapterPosition = 0;
}

// walk through adapter starting at firstViewAdapterPosition stacking each vended item
for (int adapterPosition = firstViewAdapterPosition; adapterPosition < state.getItemCount(); adapterPosition++) {
Expand Down

0 comments on commit 4b9dc86

Please sign in to comment.