android - ListView CAB勾选项着色

标签 android android-listview contextual-action-bar

我很茫然,因为我觉得 android 应该处理这个默认选择行为......

我将 actionbarsherlock 与 ListView 一起使用,并在长按项目后出现上下文操作栏。我希望这些项目在长按后突出显示,但它们只是进行初始闪烁的蓝色变暗,然后恢复为默认颜色。我错过了什么吗?

    mHabitListView.setAdapter(mAdapter);
    mHabitListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mHabitListView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {

                if (mHabitListView.isItemChecked(position)){
                    mHabitListView.setItemChecked(position, false);


                } else {
                    mHabitListView.setItemChecked(position, true);

                }

                if (mHabitListView.getCheckedItemCount() > 0) {

                    if (mMode == null) {
                        mMode = startActionMode(new ModeCallback());
                    } else {
                        mMode.setTitle(mHabitListView.getCheckedItemCount() + " " + getString(R.string.cab_selected_count));

                    }
                } else {
                    if (mMode != null) {
                        mMode.finish();

                    }
                }
                return true;
            }

    });






}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.menu_add_new_habit:
            Habit h = new Habit("Floss", "GOOD", "", "");

            mDbHelper.createHabitEntry(h);
            mDbHelper.close();

            Cursor cursor =  mDbHelper.getAllEntries();
            mAdapter.changeCursor(cursor);

            break;
    }
    return true;
}



 private final class ModeCallback implements ActionMode.Callback {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Create the menu from the xml file
            MenuInflater inflater = getSupportMenuInflater();
            mode.setTitle(mHabitListView.getCheckedItemCount() + " " + getString(R.string.cab_selected_count));
            inflater.inflate(R.menu.list_contextual_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Here, you can checked selected items to adapt available actions
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // Destroying action mode, let's unselect all items
            for (int i = 0; i < mHabitListView.getAdapter().getCount(); i++)
                mHabitListView.setItemChecked(i, false);

            if (mode == mMode) {
                mMode = null;
            }
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            long[] selected = mHabitListView.getCheckedItemIds();
            if (selected.length > 0) {
                for (long id: selected) {
                    // Do something with the selected item
                }
            }
            mode.finish();
            return true;


        }


    }

最佳答案

Listitem 的背景设置为 android:background="?android:attr/activatedBackgroundIndicator"

如果您仍然遇到任何问题,请尝试从您的 ListView xml 中删除 listSelector 属性

关于android - ListView CAB勾选项着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14209707/

相关文章:

java - 指南针应用程序无法实例化 Activity

java - Android:来自单个数据的多列 ListView

java - 用户界面 : Action Bar and Contextual Action Bar, 新趋势

android - java.lang.classcastException android.content.pm.ResolveInfo

android - 当 viewpager 更改时关闭 Context Actionbar

android - 为 recyclerview 实现上下文操作栏

Android - 是否可以为静态成员和方法使用自定义类而不是扩展应用程序类

java - 如何使用文本、图像和复选框过滤自定义 ListView ?

android - Android 上的 Google map 无法加载

android - Android 中每个项目的 ListView 2 行和图标