
我已经设置了一个ActionMode回调函数,用作使用项目的ActionbarSherlock中的上下文Actionbar(CAB).
我正在尝试设置多个选择,以便我可以删除ListVIEw中的多个项目.
我注意到在调试时,当上下文Actionbar(CAB)未打开并且我在我触摸的列表项上调用isItemChecked()时,它会返回false.但是当CAB打开时,我触摸的项目(我以前没有碰过)在调用isItemChecked()时返回true.然后当我从getCheckedItemIDs()调用数组上的delete时,该数组不包含先前为isItemChecked()返回true的项.
有没有人见过这个?
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.habit); habitListVIEw = (ListVIEw)findVIEwByID(R.ID.habitsListVIEw); habitListVIEw.setAdapter(mAdapter); habitListVIEw.setChoiceMode(AbsListVIEw.CHOICE_MODE_MulTIPLE); habitListVIEw.setItemsCanFocus(false); habitListVIEw.setonItemLongClickListener(new MyOnItemLongClickListener()); habitListVIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { if (habitListVIEw.isItemChecked(position)) { // for deBUGging, returns false here // when CAB isnt up. int h = 2; } // handle differently when CAB is on. if (mMode != null) { if (habitListVIEw.isItemChecked(position)) { // returns true here when CAB is up // but this is the first time I'm // clicking the item habitListVIEw.setItemChecked(position, false); // turn CAB off if this is the last to be unchecked if (habitListVIEw.getCheckedItemIDs().length == 0) { mMode.finish(); } } else { habitListVIEw.setItemChecked(position, true); } } else { // start detail/edit vIEw activity } } });}@OverrIDepublic 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;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemID()) { case R.ID.menu_create: Habit test = new Habit("FLOSS", "GOOD", "", ""); mDbHelper.createHabitEntry(test); mAdapter.changeCursor(mDbHelper.getAllEntrIEs()); break; } return false;}private class MyOnItemLongClickListener implements OnItemLongClickListener { @OverrIDe public boolean onItemLongClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { habitListVIEw.setItemChecked(position, true); mMode = startActionMode(new MyActionModeCallback()); return true; }}private class MyActionModeCallback implements ActionMode.Callback { @OverrIDe public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // Todo auto-generated method stub return false; } @OverrIDe public voID onDestroyActionMode(ActionMode mode) { habitListVIEw.setonItemLongClickListener(new MyOnItemLongClickListener()); } @OverrIDe public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.main_long_click_context, menu); habitListVIEw.setonItemLongClickListener(null); return true; } @OverrIDe public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemID()) { case R.ID.menu_delete: long[] selected = habitListVIEw.getCheckedItemIDs(); if (selected.length > 0) { for (long ID : selected) { mDbHelper.deleteEntry(ID); } } mAdapter.changeCursor(mDbHelper.getAllEntrIEs()); mode.finish(); break; } return true; }};解决方法:
所以对我有用的就是使用
habitListVIEw.setonItemClickListener(new OnItemClickListener(){ @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { //handle differently when CAB is on. if (mMode != null){ vIEw.setSelected(!vIEw.isSelected()); updateCABTitle(); //turn CAB off if this is the last to be unchecked if (habitListVIEw.getCheckedItemIDs().length == 0){ mMode.finish(); } } else { //start detail/edit vIEw activity Intent intent = new Intent(getApplicationContext(), HabitDetailActivity.class); startActivity(intent); } } });和
private class MyOnItemLongClickListener implements OnItemLongClickListener{ @OverrIDe public boolean onItemLongClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { habitListVIEw.setItemChecked(position, true); mMode = startActionMode(new MyActionModeCallback()); updateCABTitle(); return true; }}我真的不明白为什么setItemChecked()不适用于onItemClick()但似乎是onItemLongClick().看起来好像有一个默认的点击处理程序,它在AbsListVIEw.PerformItemClick()中被调用,它必须对已检查/未选中或某事进行一些切换.
总结以上是内存溢出为你收集整理的android – 为什么isItemChecked()在ActionMode中返回true?全部内容,希望文章能够帮你解决android – 为什么isItemChecked()在ActionMode中返回true?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)