ListView的选择仍然选择退出模式后,持续模式、ListView

2023-09-12 01:07:09 作者:怕孤厌闹

我有一个ListView子类,允许我在选择时上下文操作栏(CAB)是有效的。政制事务局设置为回调至 onItemLongClick 事件:

I have a ListView subclass that I allow selections on when the context action bar (CAB) is active. The CAB is set as a callback to the onItemLongClick event:

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate a menu resource providing context menu items
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(context_menu, menu);
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    return true;
}

这是罚款,并在ListView按预期工作,用触摸时当前所选项目下榻突出。

This is fine, and the ListView works as expected, with the currently selected item staying highlighted when touched.

当我关闭CAB,我想在ListView恢复正常(即触摸模式)。问题是,最后一次选择的项目仍然无限期地强调,不管用什么方法我尝试清除它:

When I close the CAB, I want the ListView to return to normal (i.e. Touch mode). The problem is that the last selected item remains highlighted indefinitely, regardless of what methods I try to clear it:

public void onDestroyActionMode(ActionMode mode) {
    //Unselect any rows
    ListView lv = getListView();
    lv.clearChoices(); // Has no effect
    lv.setChoiceMode(ListView.CHOICE_MODE_NONE); // Has no effect on the highlighted item 
    lv.setFocusable(false); // Has no effect
    lv.setSelection(0); // Has no effect
    mActionMode = null;
}

有什么建议?

推荐答案

的主要原因的问题是,一旦的ListView 选择模式切换到 CHOICE_MODE_NONE ,该框架优化了清除操作,因为它不再支持选择。予功能提高了上述手动清零选择状态,然后设置模式以延迟的方式,以便该框架将具有轮到它清除转动模式为 CHOICE_MODE_NONE 。

The main reason for the problem is that once the ListView selection mode is switched to CHOICE_MODE_NONE, the framework optimizes out the clear operation as it is no longer supporting 'selections'. I have improved the above workarounds a bit by clearing the selection state manually and then setting the mode in a delayed manner so the framework will have its turn to clear the state before turning the mode to CHOICE_MODE_NONE.

final ListView lv = getListView();
lv.clearChoices();
for (int i = 0; i < lv.getCount(); i++)
    lv.setItemChecked(i, false);
lv.post(new Runnable() {
    @Override
    public void run() {
        lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
    }
});