Created
June 18, 2012 16:04
-
-
Save kingori/2949121 to your computer and use it in GitHub Desktop.
android AsyncTaskLoader sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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