【问题标题】:ListView CAB Checked item coloringListView CAB 检查项目着色
【发布时间】:2012-12-21 23:51:54
【问题描述】:

我很茫然,因为我觉得 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;


        }


    }

【问题讨论】:

    标签: android android-listview contextual-action-bar


    【解决方案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);
    

    【讨论】:

    • 我不会将自己限制在该布局中,我有一个正在使用的自定义列表视图行布局
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      相关资源
      最近更新 更多