ExpandListView converView复用问题 (设置子所有子条目只能单选)

条目状态错乱问题 (这个和listView RecyclerView是一样)就是条目中有 有状态的组件 如:checkBox radioButton等

在这里插入图片描述

数据集: javaBean
public class Bean implements Serializable {

    private String topTitle;
    private List<String> lvel1Titles;

    public String getTopTitle() {
        return topTitle;
    }

    public void setTopTitle(String topTitle) {
        this.topTitle = topTitle;
    }

    public List<String> getLvel1Titles() {
        return lvel1Titles;
    }

    public void setLvel1Titles(List<String> lvel1Titles) {
        this.lvel1Titles = lvel1Titles;
    }
}

public class MyAdapter extends BaseExpandableListAdapter {


    Map<String, Boolean> maps = new HashMap<>(); //converView复用导致的问题

    Map<String, CheckBox> checkBoxMap = new HashMap<>(); //单选处理

    private List<Bean> datas; //数据集

    CallBack mCallBack;

    public MyAdapter(List<Bean> datas,CallBack callBack) {
        this.datas = datas;
        this.mCallBack = callBack; //创建适配器的时候把这个回调接口赋值
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getLvel1Titles().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getLvel1Titles().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.topTitle = convertView.findViewById(R.id.tv_top);

            convertView.setTag(groupViewHolder);

        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.topTitle.setText(datas.get(groupPosition).getTopTitle());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHodler childViewHodler;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_level1, parent, false);
            childViewHodler = new ChildViewHodler();
            childViewHodler.level1CheckBox = convertView.findViewById(R.id.cb_level1);
            convertView.setTag(childViewHodler);

        } else {
            childViewHodler = (ChildViewHodler) convertView.getTag();
        }
        childViewHodler.level1CheckBox.setText(groupPosition + ":" + datas.get(groupPosition).getLvel1Titles().get(childPosition));
        childViewHodler.level1CheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for ( Map.Entry<String, CheckBox> entry:checkBoxMap.entrySet()) {
                    CheckBox value = entry.getValue();
                    value.setChecked(false);
                }
                checkBoxMap.clear();  //用完之后清一下状态
                if (isChecked) {
                    maps.put(groupPosition + "" + childPosition, true); //只有groupPosition和childPosition才能确定一个子条目
                    checkBoxMap.put(groupPosition + "" + childPosition, childViewHodler.level1CheckBox);
// 把 checkBox传给接口 (使用页面实现 getCheckBox拿到选中的 checkBox然后就可以获取值了)
                    mCallBack.getCheckBox(childViewHodler.level1CheckBox);
                } else {
                    maps.remove(groupPosition + "" + childPosition);

                }
            }
        });

        if (maps != null && maps.containsKey(groupPosition + "" + childPosition)) {

            childViewHodler.level1CheckBox.setChecked(true);
        } else {

            childViewHodler.level1CheckBox.setChecked(false);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {

        TextView topTitle;
    }


    static class ChildViewHodler {

        CheckBox level1CheckBox;
    }

    /**
     * 回调接口处理 子条目选中的的CheckBox的text
     */
    interface CallBack {
        void getCheckBox(CheckBox checkBox);
    }

}
页面中使用
public class MainActivity extends AppCompatActivity implements MyAdapter.CallBack {

    private ExpandableListView expandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.elv);


        List<Bean> datas = new ArrayList<>();

        for (int i = 0; i <  10; i++) {

            Bean bean = new Bean();

            bean.setTopTitle("上层目录"+i);

            List<String> lev1s = new ArrayList<>();

            for (int j = 0; j < 5; j++) {
                lev1s.add("子条目"+j);
            }

            bean.setLvel1Titles(lev1s);

            datas.add(bean);
        }

        MyAdapter adapter = new MyAdapter(datas,this);

        expandableListView.setAdapter(adapter);




    }

    @Override
    public void getCheckBox(CheckBox checkBox) {

        Toast.makeText(this, ""+checkBox.getText(), Toast.LENGTH_SHORT).show();
    }
}

写在最后 布局文件就不贴出来了 通过适配器的静态内部类 大家应该很清楚了。 这个功能对大家来说 可能是分分钟就搞出来了,我这里贴出来只是为了以后自己方便查阅。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值