0

カスタム 内にカスタム コンテキスト アクション バー (CAB) を作成していますWebView

私の要件は、最初の CAB から特定のオプションを選択すると、2 番目の CAB が開くようにすることです。これが最終目標です。

ユーザーが選択モードに入ると、この CAB が表示されます。 蛍光ペンをタップすると、この 2 番目の CAB が表示されます。

これを実現しましたが、選択が正しく機能していません。正しいように見えますが、ユーザーが他の 3 つのアイコンのいずれかをタップすると、次の結果になります。

その他のアイコンをタップします。 選択は、必要のないときにぶらぶらします。

ユーザーが 4 つの初期アイコンのいずれかを押した後に選択をタッチすると、null ポインター例外が原因でアプリがクラッシュします。

ユーザーがアクションを選択した後に残っている選択に触れると、アプリはアイコンを使用して CAB を作成したActionMode(それを と呼びましょうfirstActionMode) にアクセスして、CAB を無効化/更新しようとします。ただし、firstActionMode色付きの CAB が作成されると破棄され (それを呼び出しますsecondActionMode)、null ポインター例外が発生します。


clearFocus()選択を解除するために、メソッド内で呼び出すとonDestroyActionMode(firstActionMode)うまくいくことがわかりました。

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

ただし、これを実装すると、secondActionModeその CAB を作成するときに選択が保持されません。

前と同じだけど… いずれかのアイコンをクリックすると、選択が消えます。

この点から色を選択すると、実際に目的の機能が生成されます。ただし、これは「機能します」が、(最も残念なことに)私の要件を満たしていません。によって作成されたCABが表示されているときに、選択を表示して機能secondActionModeさせる必要があります。


カスタム クラス ( WebView) とそのネストされたActionModes のコードは次のとおりです。

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

}


これはどのように修正できますか?どんな助けでも大歓迎です。

4

1 に答える 1

0

この問題を解決しました。私はそれを考えすぎていました。解決策は ではなくActionMode、メニューにあります。

まったく新しいActionModeカラー メニューを開始する代わりに、蛍光ペンが選択されている場合はアイコン メニューを変更します。Menuパラメータへの参照を保存し、onCreateActionMode何らかのフラグを設定します。私はブール値を使用しています。

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

選択のハイライトとハンドルはアクティブで機能的なままであり、色のオプションは意図したとおりに機能します。

ActionModeCallback2 番目のクラスを削除しても安全です。それは無価値です。繰り返しますが、考えすぎです。:P

于 2014-04-16T19:59:04.587 に答える