android: smoothScrollToPosition() not working correctly

AndroidListviewSelectionAndroid ArrayadapterSmooth Scrolling

Android Problem Overview


I'm trying to smoothly scroll to last element of a list after adding an element to the arrayadapter associated with the listview. The problem is that it just scrolls to a random position

arrayadapter.add(item);
//DOES NOT WORK CORRECTLY:
listview.smoothScrollToPosition(arrayadapter.getCount()-1);

//WORKS JUST FINE:
listview.setSelection(arrayadapter.getCount()-1);

Android Solutions


Solution 1 - Android

You probably want to tell the ListView to post the scroll when the UI thread can handle it (which is why yours it not scrolling properly). SmoothScroll needs to do a lot of work, as opposed to just go to a position ignoring velocity/time/etc. (required for an "animation").

Therefore you should do something like:

    getListView().post(new Runnable() {
		@Override
		public void run() {
			getListView().smoothScrollToPosition(pos);
		}
	});

Solution 2 - Android

(Copied from my answer: https://stackoverflow.com/questions/14479078/smoothscrolltopositionfromtop-is-not-always-working-like-it-should/20997828#20997828)

This is a known bug. See https://code.google.com/p/android/issues/detail?id=36062

However, I implemented this workaround that deals with all edge cases that might occur:

First call smothScrollToPositionFromTop(position) and then, when scrolling has finished, call setSelection(position). The latter call corrects the incomplete scrolling by jumping directly to the desired position. Doing so the user still has the impression that it is being animation-scrolled to this position.

I implemented this workaround within two helper methods:

smoothScrollToPosition()

public static void smoothScrollToPosition(final AbsListView view, final int position) {
    View child = getChildAtPosition(view, position);
    // There's no need to scroll if child is already at top or view is already scrolled to its end
    if ((child != null) && ((child.getTop() == 0) || ((child.getTop() > 0) && !view.canScrollVertically(1)))) {
        return;
    }
        
    view.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(final AbsListView view, final int scrollState) {
            if (scrollState == SCROLL_STATE_IDLE) {
                view.setOnScrollListener(null);

                // Fix for scrolling bug
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        view.setSelection(position);
                    }
                });
            }
        }

        @Override
        public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount,
                                 final int totalItemCount) { }
    });

    // Perform scrolling to position
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            view.smoothScrollToPositionFromTop(position, 0);
        }
    });
}

getChildAtPosition()

public static View getChildAtPosition(final AdapterView view, final int position) {
    final int index = position - view.getFirstVisiblePosition();
    if ((index >= 0) && (index < view.getChildCount())) {
        return view.getChildAt(index);
    } else {
        return null;
    }
}

Solution 3 - Android

You should use setSelection() method.

Solution 4 - Android

Use LayoutManager to smooth scroll

layoutManager.smoothScrollToPosition(recyclerView, new RecyclerView.State(), position);

Solution 5 - Android

final Handler handler = new Handler();
//100ms wait to scroll to item after applying changes
handler.postDelayed(new Runnable() {
@Override
public void run() {
   listView.smoothScrollToPosition(selectedPosition);
}}, 100);

Solution 6 - Android

The set selection method mentioned by Lars works, but the animation was too jumpy for our purposes as it skips whatever was left. Another solution is to recall the method repeatedly until the first visible position is your index. This is best done quickly and with a limit as it will fight the user scrolling the view otherwise.

private  void DeterminedScrollTo(Android.Widget.ListView listView, int index, int attempts = 0) {
    if (listView.FirstVisiblePosition != index && attempts < 10) {
		attempts++;
		listView.SmoothScrollToPositionFromTop (index, 1, 100);
		listView.PostDelayed (() => {
			DeterminedScrollTo (listView, index, attempts);
		}, 100);
	}
}

Solution is in C# via. Xamarin but should translate easily to Java.

Solution 7 - Android

Do you call arrayadapter.notifyDataSetChanged() after you called arrayadapter.add()? Also to be sure, smoothScrollToPosition and setSelection are methods available in ListView not arrayadapter as you have mentioned above.

In any case see if this helps: https://stackoverflow.com/questions/6942582/smoothscrolltoposition-after-notifydatasetchanged-not-working-in-android

Solution 8 - Android

use this in android java, it work for me:

private void DeterminedScrollTo(int index, int attempts) {
        if (listView.getFirstVisiblePosition() != index && attempts < 10) {
            attempts++;
            if (listView.canScrollVertically(pos))
                listView.smoothScrollToPositionFromTop(index, 1, 200);
            int finalAttempts = attempts;
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    DeterminedScrollTo(index, finalAttempts);
                }
            }, 200);
        }
    }

Solution 9 - Android

Solution in kotlin:

fun AbsListView.scrollToIndex(index: Int, duration: Int = 150) {
    smoothScrollToPositionFromTop(index, 0, duration)
    postDelayed({
        setSelection(index)
        post { smoothScrollToPositionFromTop(index, 0, duration) }
    }, duration.toLong())
}

PS: Looks like its quite messed up on Android SDK side so this is kind of best you can get, if you don't want to calculate your view offset manually. Maybe best easy way is to set duration to 0 for long list to avoid any visible jump.

I had some issues when calling just setSelection in some positions in GridView so this really seems to me as solution instead of using that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1515520View Question on Stackoverflow
Solution 1 - AndroidMartin MarconciniView Answer on Stackoverflow
Solution 2 - AndroidLars BlumbergView Answer on Stackoverflow
Solution 3 - AndroidTaoZangView Answer on Stackoverflow
Solution 4 - AndroidSiddhesh ShirodkarView Answer on Stackoverflow
Solution 5 - AndroidVSBView Answer on Stackoverflow
Solution 6 - AndroidDaniel RobertsView Answer on Stackoverflow
Solution 7 - AndroidCode PoetView Answer on Stackoverflow
Solution 8 - AndroidSaeed KhaliliView Answer on Stackoverflow
Solution 9 - AndroidRenetikView Answer on Stackoverflow