@@ -312,14 +312,74 @@ public void selected(final CommentsInfoItem selectedItem) {
312312 });
313313
314314 itemsList .clearOnScrollListeners ();
315- itemsList .addOnScrollListener (new OnScrollBelowItemsListener () {
315+
316+ /*
317+ * Add initial scroll listener - which tries to load more items when not enough
318+ * are in the view (not scrollable) and more are available.
319+ *
320+ * Note: This method only works because "This callback will also be called if visible
321+ * item range changes after a layout calculation. In that case, dx and dy will be 0."
322+ * - which might be unexpected because no actual scrolling occurs...
323+ *
324+ * This listener will be replaced by DefaultItemListOnScrolledDownListener when
325+ * * the view was actually scrolled
326+ * * the view is scrollable
327+ * * No more items can be loaded
328+ */
329+ itemsList .addOnScrollListener (new DefaultItemListOnScrolledDownListener () {
316330 @ Override
317- public void onScrolledDown (final RecyclerView recyclerView ) {
318- onScrollToBottom ();
331+ public void onScrolled (final RecyclerView recyclerView , final int dx , final int dy ) {
332+ super .onScrolled (recyclerView , dx , dy );
333+
334+ if (dy != 0 ) {
335+ log ("Vertical scroll occurred" );
336+
337+ useNormalScrollListener ();
338+ return ;
339+ }
340+ if (isLoading .get ()) {
341+ log ("Still loading data -> Skipping" );
342+ return ;
343+ }
344+ if (!hasMoreItems ()) {
345+ log ("No more items to load" );
346+
347+ useNormalScrollListener ();
348+ return ;
349+ }
350+ if (itemsList .canScrollVertically (1 )
351+ || itemsList .canScrollVertically (-1 )) {
352+ log ("View is scrollable" );
353+
354+ useNormalScrollListener ();
355+ return ;
356+ }
357+
358+ log ("Loading more data" );
359+ loadMoreItems ();
360+ }
361+
362+ private void useNormalScrollListener () {
363+ log ("Unregistering and using normal listener" );
364+ itemsList .removeOnScrollListener (this );
365+ itemsList .addOnScrollListener (new DefaultItemListOnScrolledDownListener ());
366+ }
367+
368+ private void log (final String msg ) {
369+ if (DEBUG ) {
370+ Log .d (TAG , "itemListInitScrollListener - " + msg );
371+ }
319372 }
320373 });
321374 }
322375
376+ class DefaultItemListOnScrolledDownListener extends OnScrollBelowItemsListener {
377+ @ Override
378+ public void onScrolledDown (final RecyclerView recyclerView ) {
379+ onScrollToBottom ();
380+ }
381+ }
382+
323383 private void onStreamSelected (final StreamInfoItem selectedItem ) {
324384 onItemSelected (selectedItem );
325385 NavigationHelper .openVideoDetailFragment (requireContext (), getFM (),
@@ -407,66 +467,7 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
407467 // Load and handle
408468 //////////////////////////////////////////////////////////////////////////*/
409469
410- /**
411- * If more items are loadable and the itemList is not scrollable -> load more data.
412- * <br/>
413- * Should be called once the initial items inside {@link #startLoading(boolean)}
414- * has been loaded and added to the {@link #itemsList}.
415- * <br/>
416- * Otherwise the loading indicator is always shown but no data can be loaded
417- * because the view is not scrollable; see also #1974.
418- */
419- protected void ifMoreItemsLoadableLoadUntilScrollable () {
420- ifMoreItemsLoadableLoadUntilScrollable (0 );
421- }
422-
423- /**
424- * If more items are loadable and the itemList is not scrollable -> load more data.
425- *
426- * @param recursiveCallCount Amount of recursive calls that occurred
427- * @see #ifMoreItemsLoadableLoadUntilScrollable()
428- */
429- protected void ifMoreItemsLoadableLoadUntilScrollable (final int recursiveCallCount ) {
430- // Try to prevent malfunction / stackoverflow
431- if (recursiveCallCount > 100 ) {
432- Log .w (TAG , "loadEnoughInitialData - Too many recursive calls - Aborting" );
433- return ;
434- }
435- if (!hasMoreItems ()) {
436- if (DEBUG ) {
437- Log .d (TAG , "loadEnoughInitialData - OK: No more items to load" );
438- }
439- return ;
440- }
441- if (itemsList .canScrollVertically (1 )
442- || itemsList .canScrollVertically (-1 )) {
443- if (DEBUG ) {
444- Log .d (TAG , "loadEnoughInitialData - OK: itemList is scrollable" );
445- }
446- return ;
447- }
448- if (DEBUG ) {
449- Log .d (TAG , "loadEnoughInitialData - View is not scrollable "
450- + "but it could load more items -> Loading more" );
451- }
452- loadMoreItems (() ->
453- ifMoreItemsLoadableLoadUntilScrollable (recursiveCallCount + 1 ));
454- }
455-
456- /**
457- * Loads more items.
458- * @param initialDataLoadCallback
459- * Callback used in {@link #ifMoreItemsLoadableLoadUntilScrollable()}.
460- * <br/>
461- * Execute it once the data was loaded and added to the {@link #itemsList}.
462- * <br/>
463- * Might be <code>null</code>.
464- */
465- protected abstract void loadMoreItems (@ Nullable Runnable initialDataLoadCallback );
466-
467- protected void loadMoreItems () {
468- loadMoreItems (null );
469- }
470+ protected abstract void loadMoreItems ();
470471
471472 protected abstract boolean hasMoreItems ();
472473
0 commit comments