为什么isItemChecked()在ActionMode中返回true?

时间:2013-01-14 23:45:45

标签: android android-listview actionbarsherlock

我已设置ActionMode回调用作ActionBar使用项目中的上下文ActionBarSherlock(CAB)。

我正在尝试设置多个选择,以便我可以删除ListView中的多个项目。

我注意到在调试时,当上下文ActionBar(CAB)未打开并且我在我触摸的列表项上调用isItemChecked()时,它会返回false。但是当CAB打开时,我触摸的项目(我以前没有碰过)会在调用isItemChecked()时返回true。然后,当我从getCheckedItemIds()调用数组上的delete时,该数组不包含先前为isItemChecked()返回true的项。

有没有人见过这个?

@Override
protected 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
            }
        }
    });
}

@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_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;
    }
};

1 个答案:

答案 0 :(得分:2)

所以对我有用的就是使用

   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()中被调用,必须进行一些切换检查/取消检查或其他东西。