5

友達、

ChildView で単一選択チェックボックスを使用する ExpandableListView を作成しようとしています。また、ExpandableListView の OnChildClickListener() で他の CheckBoxes を "false" に設定する方法がわかりません。これが私のコードです:

 ExpListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
                if (cb.isChecked()) {           

                } else {
                    cb.setChecked(true);
                    //Here somehow I must set all other checkboxes to false.
                            //Is it possible?
                }
                return false;
            }
   });

ここに ChildView のxmlがあります:

 <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/textChild"
     android:layout_width="wrap_content"
     android:layout_height="40dp"
     android:layout_marginLeft="20dp"
     android:layout_marginTop="20dp"
     android:textColor="@android:color/white"
     android:layout_weight="1"
     />

<CheckBox android:id="@+id/checkbox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:clickable="false" 
      android:layout_gravity="right" 
      android:visibility="visible"
/> 

</LinearLayout>
4

1 に答える 1

5

チェックボックスを 1 つだけ選択できるようにしたい場合は、チェックしたチェックボックスを variable に保存できますCheckBox checkedBox;。をクリックするCheckBoxと、次の行に沿って何かを行うことができます

@Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            CheckBox last = checkedBox  //Defined as a field in the adapter/fragment
            CheckBox current = (CheckBox) v.findViewById(R.id.checkbox);

            last.setCheked(false);    //Unchecks previous, checks current
            current.setChecked(true); // and swaps the variable, making 
            checkedBox = current;     // the recently clicked `checkedBox`

            return false;
        }

ただし、これが Android のビュー リサイクル システムで機能するかどうかはわかりませんが、試してみる価値はあります。

複数の選択肢が必要な場合は、 を に展開し、checkedBoxボックスList<CheckBox>のチェックを外す必要があるたびに繰り返します。

追加のデータを格納する必要がある場合 (おそらく必要になるでしょう)、ホルダー クラスを作成できます。

class CheckBoxHolder{

    private CheckBox checkBox:
    private int id;

    public CheckBoxHolder(CheckBox cb, int id){
        this.checkBox = cb;
        this.id = id;
    }
    // Getter and/or setter, etc. 
}
于 2013-10-29T16:37:38.247 に答える