MainActivity如下:
package cn.testexpandablelistview1; import android.os.Bundle; import android.view.View; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; 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; import android.app.Activity; /** * Demo描述: * ExpandableListView的基本使用 * * 注意: * 布局文件中android:groupIndicator="@null" * 去掉ExpandableListView的箭头图标 */ public class MainActivity extends Activity { private ExpandableListView mExpandableListView; private ExpandableListAdapter mExpandableListviewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mExpandableListView=(ExpandableListView) findViewById(R.id.expandableListView); mExpandableListviewAdapter = new ExpandableListAdapter(MainActivity.this); mExpandableListView.setAdapter(mExpandableListviewAdapter); //为ExpandableListview监听子项单击事件 mExpandableListView.setOnChildClickListener(new OnChildClickListenerImpl()); //为ExpandableListview监听组项单击事件 mExpandableListView.setOnGroupClickListener(new OnGroupClickListenerImpl()); //为ExpandableListview监听关闭分组事件 mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl()); //为ExpandableListview监听展开分组事件 mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListenerImpl()); } // 监听子项点击事件 class OnChildClickListenerImpl implements OnChildClickListener { @Override public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) { // 组的数量 int gourpsSum = mExpandableListviewAdapter.getGroupCount(); for (int i = 0; i < gourpsSum; i++) { // 每组中子项的数量 int childSum = mExpandableListviewAdapter.getChildrenCount(i); for (int k = 0; k < childSum; k++) { boolean isLastChild = false; if (k == (childSum - 1)) { isLastChild = true; } View childView=mExpandableListviewAdapter.getChildView(i, k, isLastChild, null, null); CheckBox checkBox = (CheckBox) childView.findViewById(R.id.checkBox); // 切换CheckBox状态!!! checkBox.toggle(); boolean itemIsCheck = checkBox.isChecked(); TextView textView = (TextView) childView.findViewById(R.id.textView); String gameName = textView.getText().toString(); // 实现单选 if (i == groupPosition && k == childPosition) { ExpandableListAdapter.statusHashMap.put(gameName, itemIsCheck); } else { ExpandableListAdapter.statusHashMap.put(gameName, false); } // 通知数据发生变化 ((BaseExpandableListAdapter) mExpandableListviewAdapter).notifyDataSetChanged(); } } return true; } } // 组被点击事件 class OnGroupClickListenerImpl implements OnGroupClickListener { @Override public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) { return false; } } // 组收缩事件 class OnGroupCollapseListenerImpl implements OnGroupCollapseListener { @Override public void onGroupCollapse(int groupPosition) { } } // 打开组事件 class OnGroupExpandListenerImpl implements OnGroupExpandListener { @Override public void onGroupExpand(int groupPosition) { } } }
ExpandableListAdapter如下:
package cn.testexpandablelistview1; import java.util.HashMap; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.TextView; public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context = null; public String[] groups = { "单价", "颜色" }; public String[][] children = { { "一块", "二块", "三块" },{ "黄色", "绿色", "白色" } }; public static HashMap<String, Boolean> statusHashMap; private TextView childTextView; private CheckBox childBox; public ExpandableListAdapter(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]; } // 取得子项ID @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //点击事件发生后:先执行事件监听,然后调用此getChildView() @Override public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// 返回子项组件 if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.expandablelistviewitem, null); } childTextView = (TextView) convertView.findViewById(R.id.textView); childTextView.setText(getChild(groupPosition, childPosition).toString()); childBox = (CheckBox) convertView.findViewById(R.id.checkBox); //获取当前子项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; } // 取得组ID @Override public long getGroupId(int groupPosition) { return groupPosition; } // 取得组显示组件 @Override public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) { // 建立TextView组件 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; } // 建立TextView,用于显示组 public TextView buildTextView() { AbsListView.LayoutParams param = new AbsListView.LayoutParams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); TextView textView = new TextView(this.context); textView.setLayoutParams(param); textView.setTextSize(25.0f); textView.setGravity(Gravity.LEFT); textView.setPadding(40, 8, 3, 3); return textView; } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ExpandableListView android:id="@+id/expandableListView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:groupIndicator="@null" /> </RelativeLayout>
expandablelistviewitem.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>