退出选择模式后,ListView 选择保持不变 [英] ListView selection remains persistent after exiting choice mode

查看:45
本文介绍了退出选择模式后,ListView 选择保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView 子类,允许在上下文操作栏 (CAB) 处于活动状态时进行选择.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);
    }
});

这篇关于退出选择模式后,ListView 选择保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆