public
Created

android AsyncTaskLoader sample

  • Download Gist
SampleEndlessLoader.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.