嵌套上下文动作条 [英] Nested Contextual Action Bars

查看:200
本文介绍了嵌套上下文动作条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个自定义的范围内创建自定义的上下文的动作条(CAB)的WebView

I am creating custom contextual action bars (CAB) within a custom WebView.

我的要求是这样的,从第一CAB选择特定选项将打开第二CAB。这是最终目标:

My requirements are such that selecting a particular option from the first CAB will open a second CAB. This is the end goal:


我做了这一点,但选择工作不正常。我知道这看起来是正确的,但如果用户点击任何其他三个图标,这就是结果:

I have made this happen, but the selection is not working correctly. I know it looks correct, but if the user taps any of the other three icons, this is the result:


如果用户pressing任何的四个初始图标后触及的选择,应用程序崩溃是由于一个空指针异常。

当用户触摸选择一个动作后挥之不去的选择,应用程序试图访问 ActionMode (姑且称之为 firstActionMode )创建的图标的CAB,以无效/刷新。但是,创建与颜色CAB当 firstActionMode 被破坏,(称之为 secondActionMode )导致空指针异常

When the user touches the lingering selection after selecting an action, the app tries to access the ActionMode (let's call it firstActionMode) that created the CAB with the icons in order to invalidate/refresh it. However, firstActionMode is destroyed when the CAB with the colors is created, (call that secondActionMode) causing a null pointer exception.

为了扫清选择,我发现,调用 clearFocus() onDestroyActionMode(firstActionMode)方法做这项工作就好了。

In order to clear away the selection, I have found that calling clearFocus() within onDestroyActionMode(firstActionMode) method does the job just fine.

private class CustomActionModeCallback extends ActionMode.Callback {
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        clearFocus();
    }
}

然而,当此实施后,选择并不时 secondActionMode 创建其CAB坚持:


从此点选择色彩实际上产生所需的功能。然而,尽管这个作品,它(最不幸)不符合我的要求。我需要的选择,以保持可见和功能时,所示 secondActionMode 创建的CAB。

Choosing a color from this point actually produces the desired functionality. However, while this "works," it (most unfortunately) does not meet my requirements. I need the selection to remain visible and functional when the CAB created by secondActionMode is shown.



下面是我的自定义类code(的WebView )及其嵌套的 ActionMode 取值

public class CustomWebView extends WebView {

    private ActionMode.Callback mActionModeCallback;

    @Override
    public ActionMode startActionMode(Callback callback) {
        ViewParent parent = getParent();
        if (parent == null) {
            return null;
        }
        if (callback instanceof HighlightActionModeCallback) {
            mActionModeCallback = callback;
        } else {
            mActionModeCallback = new CustomActionModeCallback();
        }       
        return parent.startActionModeForChild(this, mActionModeCallback);
    }

    private class CustomActionModeCallback implements ActionMode.Callback {
        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.first_menu, menu);
            return true;
        }

        // Called each time the action mode is shown.
        // Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // This method is called when the handlebars are moved.
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId()) {
            case R.id.copy:
                // Do stuff
                break;
            case R.id.bookmark:
                // Do stuff
                break;

            case R.id.highlight:
                startActionMode(new HighlightActionModeCallback());
                break;

            case R.id.note:
                // Do stuff
                break;
            default:
                return false;
            }

            mode.finish(); // Action picked, so close the CAB
            return true;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            clearFocus(); // This is commented in the first four screens.
        }
    }

    private class HighlightActionModeCallback implements ActionMode.Callback {

        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.highlight_colors, menu);
            return true;
        }

        // Called each time the action mode is shown.
        // Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // This method is called when the handlebars are moved.
            return false; // Return false if nothing is done
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            mode.finish(); // Action picked, so close the CAB
            return true;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // Remove the selection highlight and handles.
            clearFocus();
        }
    }

}



这怎么能解决吗?任何及所有帮助AP preciated。


How can this be fixed? Any and all help is appreciated.

推荐答案

我现在已经解决了这个问题;我用思维吧。解决办法不在于一个 ActionMode ,但内部的菜单。

I have now solved this problem; I was over-thinking it. The solution lies not in an ActionMode, but within the menu.

而不是启动一个全新的 ActionMode 的彩色菜单,更改图标菜单如果选择了荧光笔。保存 onCreateActionMode 菜单参数的引用,并设置某种标志;我使用的是一个布尔值。

Instead of starting an entirely new ActionMode for the color menu, change the icon menu if the highlighter is selected. Save a reference to the Menu parameter in onCreateActionMode and set a flag of some sort; I'm using a boolean.

private class CustomActionModeCallback implements ActionMode.Callback {

    private boolean highlighterClicked = false;
    private Menu mMenu;

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mMenu = menu;
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.first_menu, menu);
        return true;
    }

    // Called each time the action mode is shown.
    // Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // This method is called when the handlebars are moved.
        MenuInflater inflater = mode.getMenuInflater();
        if (highlighterClicked) {
            menu.clear(); // Remove the four icons
            inflater.inflate(R.menu.highlight_colors, menu); // Show the colors
            return true; // This time we did stuff, so return true
        }
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

        highlighterClicked = false;
        switch (item.getItemId()) {
        case R.id.copy:
            // Do stuff
            break;
        case R.id.bookmark:
            // Do stuff
            break;

        case R.id.highlight:
            highlighterClicked = true;
            onPrepareActionMode(mode, mMenu);
            return true;

        case R.id.note:
            // Do stuff
            break;
        default:
            // Any of the colors were picked
            return true;
        }

        mode.finish(); // Action picked, so close the CAB
        return true;
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        clearFocus();
    }
}

高亮选择并处理保持活跃和功能,并按照预期的颜色选项工作。

The selection highlight and handles stay active and functional, and the color options work as intended.

它是安全的删除第二个 ActionModeCallback 类;它是毫无价值的。再次,一路过来,想。 :P

It is safe to delete the second ActionModeCallback class; it is worthless. Again, way over-thought. :P

这篇关于嵌套上下文动作条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆