private void loadLoop ( ) throws InterruptedException { boolean firstItem = true ; boolean moreAvailable = true ; // Loop until all loaded. while ( moreAvailable ) { // Thread interruptions terminate the loop. if ( currentThread ( ) . isInterrupted ( ) ) { throw new InterruptedException ( ) ; } try { setLoading ( true ) ; // Load next increment of items. final Result < ? extends T > result = load ( ) ; moreAvailable = result != null && result . getRemaining ( ) > 0 ; setAvailable ( result != null ? result . getRemaining ( ) : 0 ) ; // If invalidated while shown, we lazily clear the data so the user doesn't see blank data while loading. final boolean needToClear = firstItem ; firstItem = false ; runOnUiThread ( new Runnable ( ) { @ Override public void run ( ) { List < ? extends T > elements = result != null ? result . getElements ( ) : Collections . < T > emptyList ( ) ; if ( needToClear ) { overwriteResult ( elements ) ; } else { appendResult ( elements ) ; } setLoading ( false ) ; } } ) ; } catch ( InterruptedException e ) { throw e ; } catch ( InterruptedIOException e ) { InterruptedException interruptedException = new InterruptedException ( ) ; interruptedException . initCause ( e ) ; throw interruptedException ; } catch ( final Throwable e ) { runOnUiThread ( new Runnable ( ) { @ Override public void run ( ) { notifyError ( e ) ; } } ) ; mError = true ; setLoading ( false ) ; } // Block until instructed to continue, even if an error occurred. // In this case, loading must be explicitly resumed. block ( ) ; } }
Loads each increment until full range has been loading halting in between increment until instructed to proceed .