ExpandableListView的使用及结合RadioButton的坑

本文详细介绍了ExpandableListView的基本使用,包括MainActivity、adapter、Main、group和child的布局。强调了在使用中需注意的事项,如设置CheckBox以正确监听ChildClick事件,以及在数据结构中使用Map的灵活性。接着讨论了ExpandableListView结合RadioButton的实现,如何处理点击事件冲突,以及解决RadioButton在adapter复用模式下的问题。最后提到了在数据处理上的两种策略,一种通过回调函数,另一种通过代码联动ChildClick事件。文章提供了具体项目的三种情况代码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandableListView 即原生控件折叠列表,很多场景我们都需要使用,先描述一下基本的使用实例:

代码两份:MainActivity+adapter

布局三张:Main+group+child

解释一下各自的作用,MainActivity作为主运行代码和功能逻辑的调用模块,adapter解析ExpandableListView

Main是MainActivity的布局,group是ExpandableListView的第一层布局,child是ExpandableListView的第二层子布局

贴代码:

MainActivity:


package com.hjk.shiny.expandablelistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String GROUP_TEXT="group_text";
    private static final String CHILD_TITLE="child_title";
    private static final String CHILD_TEXT="child_text";
    private static final String CHILD_RADIO="child_radiobutton";
    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

    List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
    List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();

    private ExpandableListView expendableListview;
    private String checkedItem="";
    private MyAdapter mMyAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        mMyAdapter=new MyAdapter(MainActivity.this,groupData,childData,groupCheckBox,childCheckBox);
        expendableListview=(ExpandableListView)findViewById(R.id.test);
        expendableListview.setAdapter(mMyAdapter);
        //去除系统自带按钮
        expendableListview.setGroupIndicator(null);

        //去除系统自带分割线
//        expendableListview.setDivider(null);
//        //展开所有二级列表
//        int groupCount=mMyAdapter.getGroupCount();
//        for(int i=0;i<groupCount;i++){
//            expendableListview.expandGroup(i);
//        }

        //监听二级列表
        expendableListview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //关键在于view v参数,可以获取布局下任意控件
                CheckBox cbx=(CheckBox)v.findViewById(R.id.multiple_checkbox);
                cbx.toggle();

                //匹配checkbox勾选取消后在childCheckBox中的数据
                if(childCheckBox.get(groupPosition).get(childPosition).get(CHILD_RADIO)){
                    childCheckBox.get(groupPosition).get(childPosition).put(CHILD_RADIO,false);
                }else{
                    childCheckBox.get(groupPosition).get(childPosition).put(CHILD_RADIO,true);
                }

                //关于选中项
                checkedItem="";//置空
                for(int i=0;i<mMyAdapter.getGroupCount();i++){//group
                    for(int j=0;j<mMyAdapter.getChildrenCount(i);j++){//child
                        //判断checkbox的true or false
                        if(childCheckBox.get(i).get(j).get(CHILD_RADIO)){
                            checkedItem=checkedItem+groupData.get(i).get(GROUP_TEXT)+":"+
                                    childData.get(i).get(j).get(CHILD_TITLE);
                        }
                    }
                }

                Toast.makeText(MainActivity.this,checkedItem,Toast.LENGTH_SHORT).show();


                return false;
            }
        });

    }


    /**
     * 此处需要注意的是childCheckBox的个数一定要多与childData的个数,不然无法匹配
     */
    private void initData(){
        //group--4  child--3  text item
        for(int i=0;i<4;i++){
            Map<String,String>curGrpMap=new HashMap<>();//4条
            groupData.add(curGrpMap);
            curGrpMap.put(GROUP_TEXT,"第"+i+"主条目");

            List<Map<String,String>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值