ListView selection remains persistent after exiting choice mode

后端 未结 11 867
青春惊慌失措
青春惊慌失措 2020-11-29 01:45

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:

11条回答
  •  忘掉有多难
    2020-11-29 02:18

    I know this has been answered, but above answers still gave me problems with the cached/recycled views that ListView maintains, that didn't update it's state when scrolled back into view. So, the above solution changes slightly to:

        lv.clearChoices();  
    
        ArrayList list = new ArrayList();
        lv.reclaimViews(list);
        for (View view : list) {
            ((Checkable) view).setChecked(false);
        }
    
        lv.setChoiceMode(lv.CHOICE_MODE_NONE);
    

    This is better than using getChildAt(i) because that method jusg gives you the currently visble views and does not account for the internal cached views, that are not visible.

提交回复
热议问题