3

所以我现在的问题是,现在我长时间单击 ListView 中的一个项目,它会弹出一个上下文操作栏。传递给 onItemLongClick 的 id 是我想在 mActionModeCallback 的 on ActionItemClicked() 方法中使用的变量。这似乎是一个相当常见的过程,因为如果用户正在编辑项目列表,您可能希望在用户单击“编辑”或“删除”操作时以某种方式访问​​数据库中该行的 ID。

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> p, View view, int pos, long id) {

        //The id of the row in the database
        long variableThatIWantToPassToCallback = id; 
        mActionMode = getActivity().startActionMode(mActionModeCallback);
        view.setSelected(true);
        return true;
    }
});

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {}

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {}

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        //I would like access to the id of the clicked item here, NOT item.getItemId()
    }

    public void onDestroyActionMode(ActionMode mode) {}
};
4

2 回答 2

8

执行此操作的正确方法是调用函数mActionMode.setTag("1")onItemCheckedStateChanged然后从onActionItemClicked函数调用中调用mode.getTag();

于 2012-10-17T20:48:58.310 回答
1

通过扩展接口创建自己的回调ActionMode.Callback

private interface ActionCallback extends ActionMode.Callback {
        public void setClickedView(View view);
}

private ActionCallback mActionModeCallback = new ActionCallback() {

        public View mClickedView;

        public void setClickedView(View view) {
            mClickedView = view;
        }

        // 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.context_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) {
            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.menu_delete:
                    Log.v( TAG, "#onActionItemClicked ready to delete the item with id: " + mClickedView.getTag() );
                    mode.finish();      // Action picked, so close the CAB
                    return true;
                default:
                    return false;
            }   // end switch
        }

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

对于已OnLongClickListener附加的视图,以这种方式覆盖onLongClick回调。

                        @Override
                // Called when the user long-clicks on someView
                public boolean onLongClick( View view ) {
                    // proceed only when actionmode is not null
                    // otherwise overlapping action modes will be
                    // displayed
                    if( mActionMode != null ) {
                        return false;
                    }

                    mActionModeCallback.setClickedView(view);
                    // Start the CAB using the ActionMode.Callback defined above
                    mActionMode = startActionMode( mActionModeCallback );
                    view.setSelected(true);
                    return true;
                }
于 2013-12-04T07:55:44.527 回答