3

我很茫然,因为我觉得 android 应该照顾这种默认选择行为......

我正在使用带有列表视图的 actionbarsherlock,并在长按项目后出现上下文操作栏。我希望这些项目在长按后突出显示,但它们只是最初闪烁的蓝色变暗,然后它们恢复为默认颜色。我错过了什么吗?

    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;


        }


    }
4

2 回答 2

0

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

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

于 2014-09-30T20:54:17.190 回答
-1

你可以使用android.R.layout.simple_list_item_activated_2

mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_activated_2,
    null,
    new String[] { Favorite.BUS_NAME, Favorite.STATION_NAME },
    new int[] { android.R.id.text1, android.R.id.text2, }, 0);
setListAdapter(mAdapter);

在此处输入图像描述

于 2013-01-12T09:42:50.300 回答