android: smoothScrollToPosition() not working correctly

后端 未结 7 1953
心在旅途
心在旅途 2020-12-03 00:11

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 r

相关标签:
7条回答
  • 2020-12-03 01:15

    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.

    0 讨论(0)
提交回复
热议问题