单击按钮时如何关闭菜单?

时间:2014-01-08 02:55:32

标签: android gridview menu menuitem

我使用GridView并设置为MultiChoiceModeListener

当我从GridView中选择项目时,它将调用onCreateActionModeonActionItemClicked,如下面的代码。

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // TODO Auto-generated method stub       
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.actionbar_layout, null);
    mActionText = (TextView) v.findViewById(R.id.action_text);
    mActionText.setText(formatString(fileListView.getCheckedItemCount()));
    mode.setCustomView(v);
    getActivity().getMenuInflater().inflate(R.menu.action_menu, menu);

    return true;
}

menu将显示我选择了多少项,如下图所示。

The item select from Gridview and show on the menu

当我点击该按钮时,它会将我选择的项目发送给新的Fragment

以下代码适用于button

download_button = (ImageButton) view.findViewById(R.id.download_button) ;

        download_button.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {

            Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
            MainActivity.addFragment(FileBrowserFragment.this, fragment);
            menu.finish(); //can not call menu.finish();

    }

但当它转向新fragment时,menu并未消失。

单击按钮并转到新menu ???

时如何关闭fragment

2 个答案:

答案 0 :(得分:0)

认为你正在寻找完成();在ActionMode上看到这个例子:

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
        case R.id.menu_delete:
            deleteSelectedItems();
            mode.finish();
            return true;
        }
    }

答案 1 :(得分:0)

如果您想通过按钮单击完成,请将监听器注册到您的按钮,然后将finish()方法放在其中。

修改

试试这个:

   @Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    // Respond to clicks on the actions in the CAB
    switch (item.getItemId()) {
    case R.id.menu_delete:


download_button.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {

             Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
             MainActivity.addFragment(FileBrowserFragment.this, fragment);

    deleteSelectedItems();
            mode.finish();

        }
});


        return true;

}