弹出菜单出现时停止列表视图滚动

时间:2015-03-20 03:29:52

标签: android android-listview popupmenu

我有一个listview,每个项目都有一个按钮。点击一个项目的按钮,我想显示一个popupmenu有很多选项(大约8个选项)。

问题是弹出菜单出现时,列表视图会滚动到它,以便分散项目点击的注意力。 请帮忙!

我显示弹出菜单的代码:

// 'view' is the button in a row
private void showPopupMenu(final View view){ 
    PopupMenu popupMenu = new PopupMenu(getContext(), view);
    popupMenu.getMenuInflater().inflate(R.menu.menu_popup, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (mMenuItemClickListener != null){
                int position = (int) view.getTag();
                return mMenuItemClickListener.onMenuItemClick(item, (int) getItemId(position), position);
            }

            return false;
        }
    });
    popupMenu.show();
}

1 个答案:

答案 0 :(得分:0)

创建如下方法:

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(listView);