上下文操作栏不使用AppCompatActivity和工具栏

时间:2017-07-16 18:53:12

标签: android cab

我尝试使用以下代码来使用AppCompatActivity和自定义ListView。

在ListView或GridView中启用批量上下文操作

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @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(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
});

我也使用这个代码,我甚至添加了" windowActionModeOverlay"对于我正在使用的Theme.AppCompat.Light.NoActionBar,但它仍然没有用。

工具栏工具栏=(工具栏)findViewById(R.id.custom_toolbar);     setSupportActionBar(栏);

final ActionBar ab = getSupportActionBar();
if (ab != null) {
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setTitle("My List");
}

可能是我的错误?

提前致谢。

0 个答案:

没有答案