天天看點

ExpandableListView + checkbox 滾動勾選失效

ExpandableListView + checkbox在使用該組合控件的時候,遇到當勾選之後滾動控件,會發現之前選中項重新自動變回了沒有選中狀态,這個問題弄的我無從下手,不斷梳理代碼邏輯,沒有問題,斷點跟蹤,,,,就是找不到原因,靈魂在Google遊蕩中,無意間發現一篇國外文章,說是滾動自動觸發oncheckedchanged();感覺整個世界都蒙 b 了。斷點就是自動觸發,就是不知道為啥,強迫症驅使我去找問題根源,,,,反而挖坑把自己埋了,最後的解決辦法就是在 gechildview()設定oncheckedchanged監聽前,将CheckBox

oncheckedchangelistener() 事件設定為null  ,再重新監聽,貼上解決源碼,強迫症慘痛教訓,!!!!!

@Override
public View getChildView(final int groupPosition, final int childPosition
        , boolean isLastChild, View convertView, ViewGroup parent) {
    ChildHolder childHolder;
    VideoCacheT vt = (VideoCacheT) getChild(groupPosition, childPosition);
    if (convertView == null) {
        childHolder = new ChildHolder();
        childHolder.cbSelect = (CheckBox) convertView.findViewById(R.id.cache_cbSelect);
        convertView.setTag(childHolder);
    } else {
        childHolder = (ChildHolder) convertView.getTag();
    }
    childHolder.course_item_videostxt.setText(EduolGetUtil.DataForString(vt.getSection_name()));
    //編輯狀态顯示勾選框
    if (!vicbSelect) {
        childHolder.cbSelect.setVisibility(View.GONE);
    } else {
        childHolder.cbSelect.setVisibility(View.VISIBLE);
    }

    childHolder.cbSelect.setOnCheckedChangeListener(null); //然後設定CheckBox狀态,,這是重點,,,      
boolean sebln = ((getIsSelected().get(vt.getSection_id())) != null ? (getIsSelected().get(vt.getSection_id())) : false);
    childHolder.cbSelect.setChecked((sebln));

    childHolder.cbSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            VideoCacheT vct = (VideoCacheT) getChild(groupPosition, childPosition);
            if (isChecked) {
                    //點選勾選 // 檔案在 DataChange 時調用setOnCheckedChangeListener 同步
                    mSelectedDownloadSectionids.put(String.valueOf(vct.getSection_id()), String.valueOf(vct.getSection_id()));
                    //選擇下載下傳檔案
                    mOnItemSelectListener.onSelected(vct, mSelectedDownloadSectionids);
                    //标記專狀态
                    getIsSelected().put(vct.getSection_id(), true);
            } else {
                    mSelectedDownloadSectionids.remove(String.valueOf(vct.getSection_id()));
                    //不選擇
                    mOnItemSelectListener.onNoneSelect(vct, mSelectedDownloadSectionids);
                    //标記專狀态
                    getIsSelected().put(vct.getSection_id(), false);
            }
        }
    });
    setBackgroundOnClickListener(childHolder.cbSelect, convertView, vt);

    return convertView;
}