大家好 我在我的应用程序中有一个可扩展的 ListView ,我在适配器中动态地在 subview 中生成单选按钮。我对可扩展 ListView 中的 setOnChildClickListener
有要求。但不幸的是,由于单选按钮,它没有被解雇。我已尝试在动态生成的单选按钮中将 setFocusable/setclickable
设置为 false。当我这样做时,setOnChildClickListener
被触发。但我无法选择单个单选按钮。所以没有帮助。谁能帮我解决这个问题?请引用我的适配器类方法,
这是我的适配器方法
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandable_child_radio_layout, null);
RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group1);
radioGroup.setFocusable(false);
final RadioButton[] rb = new RadioButton[listDataChild.get(this.listOptionsHeader.get(groupPosition)).size()];
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < listDataChild.get(this.listOptionsHeader.get(groupPosition)).size(); i++) {
rb[i] = new RadioButton(context);
rb[0].setChecked(true);
final String childText = (String) getChild(groupPosition, i);
rb[i].setText(childText);
rb[i].setId(i + 100);
rb[i].setTag("radio-child " + childText);
fontUtils.changeFontOfRadioButtontoOpenSansRegular(rb[i]);
rb[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.letter_small));
radioGroup.addView(rb[i]);
rb[i].setFocusable(false);
}
return convertView;
}
这是我的子布局 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:gravity="left"
android:layoutDirection="ltr"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="20dp">
<RadioGroup
android:id="@+id/radio_group1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RadioGroup>
</LinearLayout>
</LinearLayout>
这是我在主 Activity 类中使用的 onClick 方法。
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
System.out.println("I am called");
expandableListView.collapseGroup(groupPosition);
return false;
}
});
最佳答案
将 return 语句作为 true 而不是 false 返回给您的监听器,并确保在设置适配器后设置它!
编辑:
因为你不能选择 rbuttons 但你的 OnClick 被触发让我们手动添加选择到点击的按钮
RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group1);
radioGroup.setFocusable(false);
final RadioButton[] rb = new RadioButton[listDataChild.get(this.listOptionsHeader.get(groupPosition)).size()];
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < listDataChild.get(this.listOptionsHeader.get(groupPosition)).size(); i++) {
rb[i] = new RadioButton(context);
// rb[0].setChecked(true);
final String childText = (String) getChild(groupPosition, i);
rb[i].setText(childText);
rb[i].setId(i + 100);
rb[i].setTag("radio-child " + childText);
fontUtils.changeFontOfRadioButtontoOpenSansRegular(rb[i]);
rb[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.letter_small));
radioGroup.addView(rb[i]);
rb[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
radioGroup.check(rb[i].getId());
//if you allow multiple checked btns then leave it like that or if you don't then you need to check if there are other buttons checked in radioGroup and then unckeck them also in this method
});
rb[i].setFocusable(false);
关于android - 没有为 ExpandableListView 触发 setOnChildClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47806726/