Android的ExpandableListView单选择复选框在ChildView [英] Android ExpandableListView with single choice CheckBox at ChildView
问题描述
朋友,
我试图写一个ExpandableListView它使用单一的选择复选框在ChildView。 而且我不知道如何设置ExpandableListView其他复选框为假的OnChildClickListener()。这是我的code:
ExpListView.setOnChildClickListener(新OnChildClickListener(){
@覆盖
公共布尔onChildClick(ExpandableListView父,视图V,
INT groupPosition,诠释childPosition,长ID){
复选框CB =(复选框)v.findViewById(R.id.checkbox);
如果(cb.isChecked()){
} 其他 {
cb.setChecked(真正的);
//这里不知何故,我必须将所有其他复选框为false。
//可能吗?
}
返回false;
}
});
下面是ChildView的XML:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
<的TextView
机器人:ID =@ + ID / textChild
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =40dp
机器人:layout_marginLeft =20dp
机器人:layout_marginTop =20dp
机器人:文字颜色=@机器人:彩色/白
机器人:layout_weight =1
/>
<复选框机器人:ID =@ + ID /复选框
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:可聚焦=假
机器人:可点击=假
机器人:layout_gravity =右
机器人:能见度=看见
/>
< / LinearLayout中>
如果你想只能选择一个复选框,你可以存储选中的复选框中的一个变量复选框checkedBox;
。当复选框
点击,你可以做线沿线的东西
@覆盖
公共布尔onChildClick(ExpandableListView父,视图V,
INT groupPosition,诠释childPosition,长ID){
复选框最后= checkedBox //定义为在适配器场/片段
复选框电流=(复选框)v.findViewById(R.id.checkbox);
last.setCheked(假); //取消选中previous,检查当前
current.setChecked(真正的); //并交换变量,使得
checkedBox =电流; //最近点击'checkedBox`
返回false;
}
虽然,我不知道这是否会与机器人查看回收系统,但它是值得一试。
如果您需要多个选择,你可以展开 checkedBox
到名单,其中,复选框>
和迭代它每次你需要取消选中框。
如果你需要存储一些额外的数据(你很可能需要),你可以做一个holder类,如:
类CheckBoxHolder {
私人复选框复选框:
私人诠释身份证;
公共CheckBoxHolder(复选框CB,INT ID){
this.checkBox = CB;
this.id = ID;
}
// getter和/或setter等。
}
Friends,
I am trying to write a ExpandableListView which use single choice checkboxes at ChildView. And I can't understand how to set other CheckBoxes to "false" in OnChildClickListener() of ExpandableListView. Here is my code:
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;
}
});
here is xml of ChildView :
<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>
If you want to only be able to select one checkbox, you could store the checked checkbox in a variable CheckBox checkedBox;
. When a CheckBox
is clicked, you could do something along the lines of
@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;
}
Though, I'm not sure if this will work with Androids view recycle system, but it's worth a shot.
If you need multiple choices, you could expand the checkedBox
into a List<CheckBox>
, and iterate over it each time you need to uncheck boxes.
If you need to store some additional data (which you most likely need to), you could make a holder class, e.g.
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.
}
这篇关于Android的ExpandableListView单选择复选框在ChildView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!