上下文菜单使用listview回收选择颜色

时间:2015-03-12 22:17:46

标签: android listview contextmenu highlighting selecteditem

所以我设法创建了一个上下文菜单栏,它根据是否选中了项目的颜色。

它仅适用于2个问题:

  • 当项目被检查时......在列表的下方,其他项目也会被检查,当它们从未被选中时(我假设它与getChildPosition上的listview回收有关)?

  • 此外,当我的上下文菜单栏关闭时......突出显示仍然存在并且我不确定如何删除它?

解决方案:在上下文菜单栏关闭时删除突出显示,并仅突出显示我选择要突出显示的内容。

有什么想法吗?

我的班级:

listview = (ListView) findViewById(R.id.lst_contacts);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        try
        {
            final int checkedCount = listview.getCheckedItemCount();
            mode.setTitle("Contacts: " + checkedCount);

            if (checked)
            {
                count = count+1;
                listview.getChildAt(position).setBackgroundColor(Color.parseColor("#6DCAEC"));
            }
            else
            {
                count = checkedCount;
                listview.getChildAt(position).setBackgroundColor(Color.parseColor("#E7E8E9"));
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.contact_context_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.delete_id:
                Toast.makeText(getBaseContext(), count + " Contacts Deselected", Toast.LENGTH_SHORT).show();
                count = 0;
                mode.finish();
            case R.id.save_id:
                Toast.makeText(getBaseContext(), count + " Contacts Saved", Toast.LENGTH_SHORT).show();
                count = 0;
                mode.finish();
            case R.id.load_id:
                Toast.makeText(getBaseContext(), count + " Contacts Loaded", Toast.LENGTH_SHORT).show();
                count = 0;
                mode.finish();

        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }
});

0 个答案:

没有答案