@@ -1062,8 +1062,9 @@ public final void addScrollableHeaderWithDelay(@NonNull final T headerItem, @Int
10621062 mHandler .postDelayed (new Runnable () {
10631063 @ Override
10641064 public void run () {
1065- if (addScrollableHeader (headerItem ) && scrollToPosition )
1066- performScroll (getGlobalPositionOf (headerItem ));
1065+ if (addScrollableHeader (headerItem ) && scrollToPosition ) {
1066+ smoothScrollToPosition (getGlobalPositionOf (headerItem ));
1067+ }
10671068 }
10681069 }, delay );
10691070 }
@@ -1084,8 +1085,9 @@ public final void addScrollableFooterWithDelay(@NonNull final T footerItem, @Int
10841085 mHandler .postDelayed (new Runnable () {
10851086 @ Override
10861087 public void run () {
1087- if (addScrollableFooter (footerItem ) && scrollToPosition )
1088- performScroll (getGlobalPositionOf (footerItem ));
1088+ if (addScrollableFooter (footerItem ) && scrollToPosition ) {
1089+ smoothScrollToPosition (getGlobalPositionOf (footerItem ));
1090+ }
10891091 }
10901092 }, delay );
10911093 }
@@ -2879,7 +2881,7 @@ public void addItemWithDelay(@IntRange(from = 0) final int position, @NonNull fi
28792881 mHandler .postDelayed (new Runnable () {
28802882 @ Override
28812883 public void run () {
2882- if (addItem (position , item ) && scrollToPosition ) performScroll (position );
2884+ if (addItem (position , item ) && scrollToPosition ) autoScrollWithDelay (position , - 1 );
28832885 }
28842886 }, delay );
28852887 }
@@ -3295,8 +3297,8 @@ private void performRemove(T item, boolean permanent) {
32953297 * Removes all items of a section, header included.
32963298 * <p>For header that is also expandable, it's equivalent to remove a single item.</p>
32973299 *
3298- * @see #removeItem(int)
32993300 * @param header the head of the section
3301+ * @see #removeItem(int)
33003302 * @since 5.0.5
33013303 */
33023304 public void removeSection (IHeader header ) {
@@ -4955,22 +4957,40 @@ private boolean hasSubItemsSelected(int startPosition, List<T> subItems) {
49554957 return false ;
49564958 }
49574959
4960+ /**
4961+ * Performs <i>safe</i> smooth scroll with a delay of {@value #AUTO_SCROLL_DELAY} ms.
4962+ *
4963+ * @param position the position to scroll to.
4964+ * @since 5.0.5
4965+ */
4966+ public void smoothScrollToPosition (final int position ) {
4967+ if (mRecyclerView != null ) {
4968+ // Must be delayed to give time at RecyclerView to recalculate positions after a layout change
4969+ mRecyclerView .postDelayed (new Runnable () {
4970+ @ Override
4971+ public void run () {
4972+ performScroll (position );
4973+ }
4974+ }, AUTO_SCROLL_DELAY );
4975+ }
4976+ }
4977+
49584978 private void performScroll (final int position ) {
49594979 if (mRecyclerView != null ) {
49604980 mRecyclerView .smoothScrollToPosition (Math .min (Math .max (0 , position ), getItemCount () - 1 ));
49614981 }
49624982 }
49634983
49644984 private void autoScrollWithDelay (final int position , final int subItemsCount ) {
4965- // Must be delayed to give time at RecyclerView to recalculate positions after an automatic collapse
4985+ // Must be delayed to give time at RecyclerView to recalculate positions after a layout change
49664986 new Handler (Looper .getMainLooper (), new Handler .Callback () {
49674987 public boolean handleMessage (Message message ) {
49684988 // #492 - NullPointerException when expanding item with auto-scroll
49694989 if (mRecyclerView == null ) return false ;
49704990 int firstVisibleItem = getFlexibleLayoutManager ().findFirstCompletelyVisibleItemPosition ();
49714991 int lastVisibleItem = getFlexibleLayoutManager ().findLastCompletelyVisibleItemPosition ();
49724992 int itemsToShow = position + subItemsCount - lastVisibleItem ;
4973- // log.v("autoScroll itemsToShow=%s firstVisibleItem=%s lastVisibleItem=%s RvChildCount=%s", itemsToShow, firstVisibleItem, lastVisibleItem, mRecyclerView.getChildCount());
4993+ // log.v("autoScroll itemsToShow=%s firstVisibleItem=%s lastVisibleItem=%s RvChildCount=%s", itemsToShow, firstVisibleItem, lastVisibleItem, mRecyclerView.getChildCount());
49744994 if (itemsToShow > 0 ) {
49754995 int scrollMax = position - firstVisibleItem ;
49764996 int scrollMin = Math .max (0 , position + subItemsCount - lastVisibleItem );
@@ -4980,7 +5000,7 @@ public boolean handleMessage(Message message) {
49805000 scrollBy = scrollBy % spanCount + spanCount ;
49815001 }
49825002 int scrollTo = firstVisibleItem + scrollBy ;
4983- // log.v("autoScroll scrollMin=%s scrollMax=%s scrollBy=%s scrollTo=%s", scrollMin, scrollMax, scrollBy, scrollTo);
5003+ // log.v("autoScroll scrollMin=%s scrollMax=%s scrollBy=%s scrollTo=%s", scrollMin, scrollMax, scrollBy, scrollTo);
49845004 performScroll (scrollTo );
49855005 } else if (position < firstVisibleItem ) {
49865006 performScroll (position );
0 commit comments