ExpandableListView结合CheckBox实现单选的完整示例

jopen 11年前

MainActivity如下:

    import java.util.HashMap;        import android.app.Activity;        import android.content.Context;        import android.os.Bundle;        import android.view.ContextMenu;        import android.view.Gravity;        import android.view.LayoutInflater;        import android.view.View;        import android.view.ViewGroup;        import android.view.ContextMenu.ContextMenuInfo;        import android.widget.AbsListView;        import android.widget.BaseExpandableListAdapter;        import android.widget.CheckBox;        import android.widget.ExpandableListAdapter;        import android.widget.ExpandableListView;        import android.widget.TextView;        import android.widget.ExpandableListView.OnChildClickListener;        import android.widget.ExpandableListView.OnGroupClickListener;        import android.widget.ExpandableListView.OnGroupCollapseListener;        import android.widget.ExpandableListView.OnGroupExpandListener;                public class ExpandableListViewTestActivity extends Activity {                    private ExpandableListView elistview = null; // 定义树型组件            private ExpandableListAdapter adapter = null; // 定义适配器对象            CheckBox childBox;            TextView childTextView;            private HashMap<String, Boolean> statusHashMap;            View childItem = null;                    @Override            public void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                super.setContentView(R.layout.main); // 默认布局管理器                this.elistview = (ExpandableListView) super.findViewById(R.id.elistview); // 取得组件                this.adapter = new MyExpandableListAdapter(this); // 实例化适配器                this.elistview.setAdapter(this.adapter); // 设置适配器                this.elistview.setOnChildClickListener(new OnChildClickListenerImpl()); // 设置子项单击事件                this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());// 设置组项单击事件                this.elistview.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());// 关闭分组事件                this.elistview.setOnGroupExpandListener(new OnGroupExpandListenerImpl()); // 展开分组事件                    }                    private class OnChildClickListenerImpl implements OnChildClickListener {// 监听子项点击事件                @Override                public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {                              int gourpsSum = adapter.getGroupCount();//组的数量                    for(int i = 0; i < gourpsSum; i++) {                        int childSum = adapter.getChildrenCount(i);//组中子项的数量                        for(int k = 0; k < childSum;k++) {                            boolean isLast = false;                            if (k == (childSum - 1)){                                 isLast = true;                            }                                                        CheckBox cBox = (CheckBox) adapter.getChildView(i, k, isLast, null, null).findViewById(R.id.checkBox);                            cBox.toggle();//切换CheckBox状态!!!!!!!!!!                            boolean itemIsCheck=cBox.isChecked();                            TextView tView=(TextView) adapter.getChildView(i, k, isLast, null, null).findViewById(R.id.textView);                            String gameName=tView.getText().toString();                            if (i == groupPosition && k == childPosition) {                                statusHashMap.put(gameName, itemIsCheck);                            } else {                                 statusHashMap.put(gameName, false);                            }                            ((BaseExpandableListAdapter) adapter).notifyDataSetChanged();//通知数据发生了变化                        }                                            }                                   return true;                }            }                    private class OnGroupClickListenerImpl implements OnGroupClickListener {// 组被点击事件                @Override                public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {                    return false;                }            }                    private class OnGroupCollapseListenerImpl implements OnGroupCollapseListener {// 组收缩事件                @Override                public void onGroupCollapse(int groupPosition) {                }            }                    private class OnGroupExpandListenerImpl implements OnGroupExpandListener {// 打开组事件                @Override                public void onGroupExpand(int groupPosition) {                }            }                    @Override            public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo) {// 处理长按事件            }                    // 自定义适配器!!!!            private class MyExpandableListAdapter extends BaseExpandableListAdapter {                private Context context = null;                public String[] groups = { "扑克", "麻将" }; // 组名称                public String[][] children = { { "斗地主", "炸金花", "推火车" },{ "四川麻将", "北京麻将", "湖南麻将" } }; // 定义子选项                        public MyExpandableListAdapter(Context context) {                    this.context = context;                    statusHashMap = new HashMap<String, Boolean>();                    for (int i = 0; i < children.length; i++) {// 初始时,让所有的子选项均未被选中                        for (int a = 0; a < children[i].length; a++) {                            statusHashMap.put(children[i][a], false);                        }                    }                }                        @Override                public Object getChild(int groupPosition, int childPosition) { // 取得指定的子项                    return this.children[groupPosition][childPosition];                }                        @Override                public long getChildId(int groupPosition, int childPosition) { // 取得子项ID                    return childPosition;                }                        public TextView buildTextView() { // 自定义方法,建立文本.用于显示组                    AbsListView.LayoutParams param = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 35); // 指定布局参数                    TextView textView = new TextView(this.context); // 创建TextView                    textView.setLayoutParams(param); // 设置布局参数                    textView.setTextSize(14.0f); // 设置文字大小                    textView.setGravity(Gravity.LEFT); // 左对齐                    textView.setPadding(40, 8, 3, 3); // 间距                    return textView; // 返回组件                }                //点击事件发生后:先执行事件监听,然后调用此getChildView()                @Override                public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// 返回子项组件                    if (convertView == null) {// 第一次的时候convertView是空,所以要生成convertView                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                        convertView = inflater.inflate(R.layout.item, null);                    }                                        childTextView = (TextView) convertView.findViewById(R.id.textView);                    childTextView.setText(getChild(groupPosition, childPosition).toString());                    childBox = (CheckBox) convertView.findViewById(R.id.checkBox);                    Boolean nowStatus = statusHashMap.get(children[groupPosition][childPosition]);//当前状态                    childBox.setChecked(nowStatus);                    return convertView;                }                        @Override                public int getChildrenCount(int groupPosition) { // 取得子项个数                    return this.children[groupPosition].length;                }                        @Override                public Object getGroup(int groupPosition) { // 取得组对象                    return this.groups[groupPosition];                }                        @Override                public int getGroupCount() { // 取得组个数                    return this.groups.length;                }                        @Override                public long getGroupId(int groupPosition) { // 取得组ID                    return groupPosition;                }                        @Override                public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// 取得组显示组件                    TextView textView = buildTextView(); // 建立组件                    textView.setText(this.getGroup(groupPosition).toString()); // 设置文字                    return textView;                }                        @Override                public boolean hasStableIds() {                    return true;                }                        @Override                public void notifyDataSetChanged() {//通知数据发生变化                    super.notifyDataSetChanged();                }                        @Override                public boolean isChildSelectable(int groupPosition, int childPosition) {                    return true;                }            }                }  
main.xml如下:
    <?xml version="1.0" encoding="utf-8"?>        <LinearLayout             xmlns:android="http://schemas.android.com/apk/res/android"            android:orientation="vertical"             android:layout_width="fill_parent"            android:layout_height="fill_parent">            <ExpandableListView                 android:id="@+id/elistview"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:groupIndicator="@null"                />        </LinearLayout>  
item.xml如下:
    <?xml version="1.0" encoding="utf-8"?>        <!-- 注意: 将复选框设置为不可点击,不可在触摸时点击,不可获得焦点 -->        <LinearLayout             xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="horizontal" >            <TextView                 android:id="@+id/textView"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="60dip"            />           <CheckBox               android:id="@+id/checkBox"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:clickable="false"               android:focusable="false"               android:focusableInTouchMode="false"               android:layout_marginLeft="100dip"            />          </LinearLayout>