2012-02-27 50 views
1

我有一個由BaseExpandableListAdapter支持的ExpandableListView。子列表項是所有複選框。爲什麼摺疊ExpandableListView組取消選中我的CheckBoxes?

我展開一個組,選中一個複選框,然後摺疊該組。該複選框然後未選中。

我的日誌輸出是:

-- I check the checkbox: 
02-27 13:51:29.674: D/ExpandableListAdapter(3583): Option '2' CHECKED 
--- I collapse the group, which unchecks the checkbox: 
02-27 13:51:30.850: D/ExpandableListAdapter(3583): Option '2' UNCHECKED 

正如你可以看到,倒塌一組調用(可能是間接的)我在該組中所有的複選框的OnCheckedChangedListener。

我的代碼是:

public class TestExpandableListActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ExpandableListView elv = 
      (ExpandableListView) findViewById(R.id.optionsELV); 
     ExpandableListAdapter ela = new ExpandableListAdapter(this); 
     elv.setAdapter(ela); 
    } 
    public class ExpandableListAdapter extends BaseExpandableListAdapter { 
     private static final String TAG = "ExpandableListAdapter"; 
     private final String[] groups; 
     private final String[][] children; 
     private final boolean[][] childrenChecked; 
     private final Context context; 
     public ExpandableListAdapter(Context c) { 
      this.context = c; 
      groups = new String[] { "Options A", "Options B" }; 
      children = 
       new String[][] {{"A", "B", "C"},{"1", "2", "3"}}; 
      childrenChecked = 
       new boolean[][] {{false, false, false},{false, false, false}}; 
     } 
     public View getChildView(
       final int groupPos, 
       final int childPos, 
       boolean isLastChild, 
       View convertView, 
       ViewGroup parent) { 
      CheckBox cb = new CheckBox(context); 
      cb.setText(children[groupPos][childPos]); 
      cb.setChecked(childrenChecked[groupPos][childPos]); 
      cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       public void onCheckedChanged(
         CompoundButton buttonView, 
         boolean isChecked) { 
        Log.d(TAG, 
          String.format("Option '%s' %s", 
            children[groupPos][childPos], 
            isChecked ? "CHECKED" : "UNCHECKED")); 
        childrenChecked[groupPos][childPos] = isChecked; 
       } 
      }); 
      return cb; 
     } 
     public int getGroupCount() { 
      return groups.length; 
     } 
     public int getChildrenCount(int groupPos) { 
      return children[groupPos].length; 
     } 
     public String getGroup(int groupPos) { 
      return groups[groupPos]; 
     } 
     public String getChild(int groupPos, int childPos) { 
      return children[groupPos][childPos]; 
     } 
     public long getGroupId(int groupPos) { 
      return (1024 * (groupPos + 1)); 
     } 
     public long getChildId(int groupPos, int childPos) { 
      return (1024 * (groupPos + 1)) + (childPos + 1); 
     } 
     public View getGroupView(
       int groupPos, 
       boolean isExpanded, 
       View convertView, 
       ViewGroup parent) { 
      TextView tv = new TextView(context); 
      tv.setText(groups[groupPos]); 
      return tv; 
     } 
     public boolean hasStableIds() { 
      return true; 
     } 
     public boolean isChildSelectable(int groupPos, int childPos) { 
      return true; 
     } 
    } 
} 

我的佈局(請注意,我正確設置choiceMode以選擇題)是:

<?xml version="1.0" encoding="utf-8"?> 
<ExpandableListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/optionsELV" 
    android:choiceMode="multipleChoice" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

爲什麼倒塌一組取消選中所有的複選框?我怎樣才能防止這種情況發生?

回答

1

我想我發現解決方案—使用CheckedTextView而不是CheckBox。

public View getChildView(
     final int groupPos, 
     final int childPos, 
     boolean isLastChild, 
     View convertView, 
     ViewGroup parent) { 
    final CheckedTextView ctv = new CheckedTextView(context); 
    ctv.setChecked(childrenChecked[groupPos][childPos]); 
    ctv.setCheckMarkDrawable(
      ctv.isChecked() 
      ? android.R.drawable.checkbox_on_background 
      : android.R.drawable.checkbox_off_background); 
    ctv.setText(children[groupPos][childPos]); 
    ctv.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      ctv.toggle(); 
      Log.d(TAG, 
       String.format("Option '%s' %s", 
        children[groupPos][childPos], 
        ctv.isChecked() ? "CHECKED" : "UNCHECKED")); 
      childrenChecked[groupPos][childPos] = ctv.isChecked(); 
      ctv.setCheckMarkDrawable(
        ctv.isChecked() 
        ? android.R.drawable.checkbox_on_background 
        : android.R.drawable.checkbox_off_background); 
     } 
    }); 
    return ctv; 
} 

的CheckedTextView DOCO不說CheckedTextView是有用在ListView — —「在了它的setChoiceMode已被設置爲比CHOICE_MODE_NONE其他東西的ListView使用時,這是非常有用的」,但我敢不清楚CheckBox和CheckedTextView之間有什麼區別,以及爲什麼CheckBox似乎完全不起作用。

任何人都可以告訴我CheckBox和CheckedTextView之間有什麼區別,以及爲什麼CheckBoxes在ExpandableListView中的功能如此奇怪?

+0

即使在檢查的文本視圖討厭,你有沒有遇到這樣的問題呢? – Harshit 2014-02-08 20:21:03

0

我有這個相同的問題。我無法告訴你爲什麼,但是移動,展開和摺疊ExpandableListView的片段會導致複選框有時會取消選中。

解決此問題的更簡單的方法是將OnClickListener事件用於CheckBox而不是OnCheckedChange。

如果摺疊或展開文本視圖,隨機切換的變化對列表的其餘它

相關問題