停止滚动的列表视图视图、列表

2023-09-05 06:49:09 作者:爱情水深王八多

我在活动列表视图和ImageButton的。 当我点击我要到列表中的一个特定位置的ImageButton(我这样做致电:setSelection(INT位)就行了。 当用户甩列表视图,然后单击ImageButton的出现问题。 这样的例子不胜枚举到指定的位置,但继续滚动。

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.

如。当我进入一扔模式下,单击该按钮,然后我去了第四名的位置,但在ListView不断滚动,所以我最终选择了25

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

推荐答案

我有写简单的方法使用反射来停止scoll的ListView。

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) {}
}

只需拨打stopScroll(myListView);当你需要停止滚动。

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