Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop scrolling in a listview

I have in my activity a listview and an imagebutton. When I click the imagebutton I want to go to a specific position in the list (I do this by calling: setSelection(int position) on the list. The problem occurs when the user flings the listview and then clicks the imagebutton. The list goes to the specified position but continues scrolling.

eg. when I go into fling mode, click the button, then I go to the fourth position, but the listview keeps scrolling, so I end up with a selection of 25

like image 987
vanleeuwenbram Avatar asked Oct 19 '11 09:10

vanleeuwenbram


2 Answers

I have write simple method to stop scoll ListView using reflection.

private void stopScroll(AbsListView view)
{
    try
    {
        Field field = android.widget.AbsListView.class.getDeclaredField("mFlingRunnable");
        field.setAccessible(true);
        Object flingRunnable = field.get(view);
        if (flingRunnable != null)
        {
            Method method = Class.forName("android.widget.AbsListView$FlingRunnable").getDeclaredMethod("endFling");
            method.setAccessible(true);
            method.invoke(flingRunnable);
        }
    }
    catch (Exception e) {}
}

Just call stopScroll(myListView); when you need to stop scroll.

like image 121
Nik Avatar answered Oct 20 '22 17:10

Nik


// Stop scrolling
smoothScrollBy(0, 0);

Internally this will call the method that @Nik is trying to reflect.

like image 21
Bradley Campbell Avatar answered Oct 20 '22 18:10

Bradley Campbell