如何在更改FragmentTab时关闭ActionMode

时间:2012-10-10 18:34:25

标签: android-tabhost actionbarsherlock fragment

我在FragmentTabs Demo中使用ActionBarSherlock和FragmentTabs。 如何从TabHost中打开不同的FragmentTab时关闭ActionMode。

问候 derjens

指向ActionBarSherlock演示的链接:FragmentTab.java / Tab LoaderCursorSupport.java

这里是我的ActionMode的代码:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
  selectedRowId = id;
  mMode = ((SherlockFragmentActivity) getActivity()).startActionMode(new TodoActionMode());
}

private final class TodoActionMode implements ActionMode.Callback {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            boolean isLight = DashboardActivity.THEME == R.style.Theme_Sherlock_Light;

            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.todo_actionmode_menu, menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_todo_actionmode_remove:
                eintragLoeschen(selectedRowId);
                return true;
            case R.id.menu_todo_actionmode_new:
                neuerTask();
                return true;
            default:
                mMode.finish();
                return true;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {


            mMode = null;
        }
    }

private void eintragLoeschen(final long rowId) {

        //eigenen dialog erstellen
        final Dialog dialog = new Dialog(getActivity());
        dialog.setContentView(R.layout.projekttab_todolist_task_delete_dialog);
        dialog.setTitle(R.string.task_wirklich_loeschen_title);


        //OK und Abbrechen Button zuweisen
        Button dialogButtonAbbrechen = (Button) dialog.findViewById(R.id.button_abbrechen);
        Button dialogButtonOk = (Button) dialog.findViewById(R.id.button_ok);

        //wenn Button Abbrechen gedrueckt wurde
        dialogButtonAbbrechen.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss(); //schliese dialog
            }
        });

        //wenn Button Ok gedrueckt wurde        
        dialogButtonOk.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                //TODO Eintrag Löschen auf ArrayAdapter ändern
                Uri uri = ContentUris.withAppendedId(TodoTable.CONTENT_URI, todolist.get((int) rowId).getId());
                getActivity().getContentResolver().delete(uri, null, null);


                mAdapter.remove(todolist.get((int) rowId));
                mAdapter.notifyDataSetChanged();

                mMode.finish();
                dialog.dismiss();
            }
        });

        //zeige dialog
        dialog.show();
    }

1 个答案:

答案 0 :(得分:0)

您可以通过调用addOnPageChangeListener来关闭actionmode viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout){ @Override public void onPageScrollStateChanged(int state) { if(ActionModeController.mActionMode != null) ActionModeController.mActionMode.finish(); } });