不为ExpandableListView触发setOnChildClickListener

时间:2017-12-14 05:55:41

标签: android expandablelistview android-radiobutton

大家好我在我的应用程序中有一个可扩展的列表视图,我在适配器中动态生成子视图中的单选按钮。我对可扩展列表视图中的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>

这是我在主要活动类中使用的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;
        }
    });

1 个答案:

答案 0 :(得分:1)

将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);