How to handle drag-to-select of sub-menu items on Kitkat?

…衆ロ難τιáo~ 提交于 2019-12-10 10:44:16

问题


Background

I have an app which has sub menus in the action bar, for selecting the sorting type.

It works really well if you just tap the action items.

Here's how a submenu looks like on the actionBar:

The code

To make it easy to understand what I did, here's a short, simple version of just the sub menu part:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.test.MainActivity" >

    <item
        android:icon="@drawable/ic_action_collections_sort_by_size_holo_dark"
        android:title="sorting"
        app:showAsAction="ifRoom">
        <menu >
            <group
                android:checkableBehavior="single"
                android:menuCategory="container" >
                <item
                    android:id="@+id/menuItem_sortByInstallTime"
                    android:title="by install time">
                </item>
                <item
                    android:id="@+id/menuItem_sortByUpdateTime"
                    android:title="by update time">
                </item>
                <item
                    android:id="@+id/menuItem_sortByAppName"
                    android:title="by app name">
                </item>
                <item
                    android:id="@+id/menuItem_sortByPackageName"
                    android:title="by package name">
                </item>
            </group>
        </menu>
    </item>

</menu>

The problem

Starting with Kitkat (Android 4.4) , instead of clicking the items, users can hold the touch and leave it when they reach the item they want to choose.

The feature is shown here , under "Drag-to-Select". Most people don't know about this feature, but people have already reported a very annoying bug about it.

In case you try out the code above (or my app), and use this feature, you will notice that you won't be able to click the same action item again, unless you click on others first.

That's right, it's blocked till you choose something else.

I've tested this issue on a Nexus 4 with Kitkat, and can confirm it.

Looking at other apps (even Google's apps), I can notice a similar issue: even though I can't find an exact example of having a grouped sub-menu, for regular sub menus, the action item is blocked for a single time. Clicking on it again will release it.

The question

Why does this occur?

Is there anything wrong with my code?

Is this a bug?

How can I fix it?

If there is no fix, should I just create my own popup menu, or is there another workaround?


回答1:


Is this a bug?

I can recreate it too. I checked the AOSP's issue tracker and didn't find anything, but it certinaly appears to be a bug.

Why does this occur?

I think it's related to the ListPopupWindow.ForwardingListener, but I'm not certain exactly where the problem occurs right now.

How can I fix it?

Call Activity.invalidateOptionsMenu after you select a MenuItem.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menuItem_sortByInstallTime:
            // Do something
            break;
        case R.id.menuItem_sortByUpdateTime:
            // Do something
            break;
        case R.id.menuItem_sortByAppName:
            // Do something
            break;
        case R.id.menuItem_sortByPackageName:
            // Do something
            break;
        default:
            break;
    }
    invalidateOptionsMenu();
    return true;
}

Limitations

Unfortunately, it looks like this solution is only viable for menu items not placed in the action bar's overflow menu. If you'd like to follow further updates regarding this problem, you should refer to issue #69205 on the AOSP's issue tracker.



来源:https://stackoverflow.com/questions/23413116/how-to-handle-drag-to-select-of-sub-menu-items-on-kitkat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!