只需点击一下即可打开CAB

时间:2015-06-18 14:10:29

标签: android contextmenu

我有一个包含许多项目的ListView。当我长按其中一个项目时,它会弹出一个CAB,我可以选择其中一些项目并对它们执行组操作。这是花花公子。我目前在我的ListView上设置了一个MultiChoiceModeListener来实现这一点(并在下面将相关代码放到了这个效果中)。

这个CAB是否可以通过短暂的点击而不是长时间点击?

public class ViewInventoryActivity extends ActionBarActivity
{
    private ArrayList<InventoryEntry> list;
    private ListView listView;
    private ListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_inventory);

        list = getList();
        listView = (ListView) findViewById(R.id.listview_inventory);
        adapter = new ListViewAdapter(this, R.layout.listview_inventory_row, list);
        // This is a custom written adapter

        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

        listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
        {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
            {
                mode.setTitle(listView.getCheckedItemCount() + " selected");
                adapter.toggleSelection(position);
                // toggleSelection keeps track of what is currently selected
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu)
            {
                mode.getMenuInflater().inflate(R.menu.menu_view_inventory, menu);
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item)
            {
                return false;
                // This does have some logic, but it's not important here
            }

            @Override
            public void onDestroyActionMode(ActionMode mode)
            {
                adapter.removeSelection();
            }

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

0 个答案:

没有答案