ExpandableListView結合CheckBox實現單選的完整示例
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; } } } </pre>main.xml如下:<pre class="brush:xml; toolbar: true; auto-links: false;"> <?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> </pre>item.xml如下:<pre class="brush:xml; toolbar: true; auto-links: false;"> <?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> </pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!