No smooth scroll if user doesn't see it. Fixes vanilla-music/vanilla#844 (#847)

The change adds interactive check to see if smooth scroll is actually
needed. If user doesn't see the library screen there's no point in
doing smooth scroll.

Besides, for old Android versions (like 4.x)
View.getFirstVisiblePosition, View.getLastVisiblePosition calls are
working only if view is currently visible.
This commit is contained in:
Antic1tizen One 2018-10-13 20:13:57 +10:00 committed by Adrian Ulrich
parent 12824fd47b
commit e2598806ac

View File

@ -900,10 +900,12 @@ public class LibraryPagerAdapter
SharedPreferences sharedPrefs = PlaybackService.getSettings(mActivity); SharedPreferences sharedPrefs = PlaybackService.getSettings(mActivity);
boolean shouldScroll = sharedPrefs.getBoolean(PrefKeys.ENABLE_SCROLL_TO_SONG, PrefDefaults.ENABLE_SCROLL_TO_SONG); boolean shouldScroll = sharedPrefs.getBoolean(PrefKeys.ENABLE_SCROLL_TO_SONG, PrefDefaults.ENABLE_SCROLL_TO_SONG);
if(shouldScroll) { if(shouldScroll) {
int middlePos = (view.getFirstVisiblePosition() + view.getLastVisiblePosition()) / 2;
for (int pos = 0; pos < view.getCount(); pos++) { for (int pos = 0; pos < view.getCount(); pos++) {
if (view.getItemIdAtPosition(pos) == id) { if (view.getItemIdAtPosition(pos) == id) {
if (Math.abs(middlePos - pos) < 30) { // for Android < 6.x visible positions are valid only if view is shown
boolean interactive = view.isShown();
int middlePos = (view.getFirstVisiblePosition() + view.getLastVisiblePosition()) / 2;
if (interactive && Math.abs(middlePos - pos) < 30) {
view.smoothScrollToPosition(pos); view.smoothScrollToPosition(pos);
} else { } else {
view.setSelection(pos); view.setSelection(pos);