简体   繁体   English

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

[英]Stop listview scrolling when popupmenu showing up

I have a listview whose each item has a button. 我有一个listview,每个项目都有一个按钮。 When clicking an item's button, I would like to show a popupmenu has many options (about 8 options). 当单击一个项目的按钮时,我想显示一个弹出菜单,其中有很多选项(大约8个选项)。

The problem is when the popupmenu showing up, the listview scrolls along to it so it distract from what item was clicking on. 问题是当弹出菜单出现时,列表视图会滚动到它上,因此它会分散正在单击的项目的注意力。 Please help! 请帮忙!

My code for showing the popup menu: 我显示弹出菜单的代码:

// '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();
}

Create a method like below: 创建如下所示的方法:

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

Whenever/wherever you want to stop scrolling just call this method like this stopScroll(listView); 无论何时何地您想停止滚动,只需像stopScroll(listView);这样的方法调用此方法stopScroll(listView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM