android - 没有为 ExpandableListView 触发 setOnChildClickListener

标签 android expandablelistview android-radiobutton

大家好 我在我的应用程序中有一个可扩展的 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/

相关文章:

在这种情况下,android-grouping radiobuttons

java - 为什么通知不显示在 API 28 中?

java - Android week 查看如何获取下一周和上一周的所有天数

java - Fragment 中的 Android 单选按钮

java - 如何通过单击android中的父节点在expandablelistview中动态获取子元素

android - 如何使整个显示可滚动

Android RadioGroup/RadioButton 选择 - Android API 16/17 中的不同 - 无法选择 Radiobutton

java - Android - 暂停几秒钟

Android:如何在 Activity 中启动/创建布局之前执行方法

android - 单击 ExpandableListView 上的复选框不会触发 onChildClick 事件