Skip to content

Instantly share code, notes, and snippets.

@kingori
Created June 18, 2012 16:04
Show Gist options
  • Save kingori/2949121 to your computer and use it in GitHub Desktop.
Save kingori/2949121 to your computer and use it in GitHub Desktop.
android AsyncTaskLoader sample
public static class SampleEndlessLoader extends AsyncTaskLoader<List<String>> {
EventManager evManager;
int itemCount = 0;
boolean isLoading = false;
public SampleEndlessLoader(Context context, EventManager evManager) {
super(context);
Log.d(LOG_TAG, "EndlessLvFrag.SampleLoader.SampleLoader:");
this.evManager = evManager;
}
@Override
public void forceLoad() {
if (!isLoading) {
super.forceLoad();
isLoading = !isLoading;
}
}
@Override
public List<String> loadInBackground() {
Log.d(LOG_TAG, "EndlessLvFrag.onCreateLoader(...).new AsyncTaskLoader() {...}.loadInBackground:");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
result.add("item_" + itemCount++);
}
Log.d(LOG_TAG, "EndlessLvFrag.onCreateLoader(...).new AsyncTaskLoader() {...}.loadInBackground: finished");
isLoading = false;
return result;
}
@Override
protected void onStartLoading() {
Log.d(LOG_TAG, "EndlessLvFrag.SampleLoader.onStartLoading:" + isLoading);
forceLoad();
}
@Override
protected void onForceLoad() {
super.onForceLoad();
evManager.fire(new DataLoadingEvent(true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment