当ListView行包含RadioButton时,ListView setOnItemClickListener不起作用

时间:2016-11-04 15:10:00

标签: android listview

请不要说这是重复的问题,因为我在谷歌搜索并尝试每个答案然后也没有得到结果。在我的Listview行中包含一个TextView和三个单选按钮。

问题
当我点击特定行的另一侧时,Listview setOnItemClickListener工作正常,但问题是当我点击单选按钮然后setOnItemClickListener没有调用我不知道为什么?

我希望当我点击或选择任何redioButton然后调用一个方法时,我知道这可以通过适配器实现,但我希望listview onItemClick()方法中的单选按钮点击事件。

对不起英语不好,提前致谢,帮助将不胜感激

xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:padding="5dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:background="@drawable/student_row_background">

        <TextView
            android:id="@+id/tv_attendance_studentName"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight=".5"
            android:gravity="center_vertical"
            android:text="Name"
            android:textColor="@color/headerActionbar"
            android:textSize="15dp"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

        <RadioGroup
            android:id="@+id/rg_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            android:orientation="horizontal"
            android:gravity="center|right"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:descendantFocusability="blocksDescendants"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="21dp"
                android:text="P"
                android:id="@+id/radioButton_present"
                android:textColor="@color/headerActionbar"
                android:textStyle="bold"
                android:gravity="center"
                android:paddingRight="10dp"
                android:textSize="12dp"
                android:buttonTint="@color/headerActionbar"
                android:focusable="false"
                android:focusableInTouchMode="false" />

            <View
                android:layout_width="1dp"
                android:layout_height="21dp"
                android:background="@color/dialogTextColor"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="21dp"
                android:text="A"
                android:textColor="@color/headerActionbar"
                android:textStyle="bold"
                android:paddingRight="10dp"
                android:gravity="center"
                android:textSize="12dp"
                android:id="@+id/radioButton_absent"
                android:buttonTint="@color/headerActionbar"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

            <View
                android:layout_width="1dp"
                android:layout_height="21dp"
                android:background="@color/dialogTextColor"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="21dp"
                android:text="L"
                android:textColor="@color/headerActionbar"
                android:textStyle="bold"
                android:paddingRight="10dp"
                android:gravity="center"
                android:textSize="12dp"
                android:id="@+id/radioButton_leave"
                android:buttonTint="@color/headerActionbar"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

        </RadioGroup>

    </LinearLayout>

</RelativeLayout>

这是 java文件

        lv_studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                     RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present);
                     RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent);
                     RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave);

                    if (!radioButton_present.isChecked() && !radioButton_absent.isChecked()
                    && !radioButton_leave.isChecked())
                    {
                        constant.showSnackBar("Please select at least one attendance option");
                    }
                    else if(radioButton_present.isChecked())
                    {
                        constant.showSnackBar("Student is present");
                    }
                    else if (radioButton_absent.isChecked())
                    {
                        constant.showSnackBar("Student is absent");
                    }
                   else if (radioButton_leave.isChecked())
                   {
                        constant.showSnackBar("Student is leave");
                   }
             }
        });

1 个答案:

答案 0 :(得分:1)

使用View方法的onItemClick()参数查找RadioGroupRadioButton视图。然后根据要求使用该视图。

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            RadioGroup group = group = (RadioGroup) view.findViewById(R.id.rg_1);
            final RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present);
            final RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent);
            final RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave);
            group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    if (!radioButton_present.isChecked() && !radioButton_absent.isChecked()
                            && !radioButton_leave.isChecked())
                    {
                        Toast.makeText(MainActivity.this,"Please select at least one attendance option",Toast.LENGTH_LONG).show();
                    }
                    else if(radioButton_present.isChecked())
                    {
                        Toast.makeText(MainActivity.this,"Student is present",Toast.LENGTH_LONG).show();
                    }
                    else if (radioButton_absent.isChecked())
                    {
                        Toast.makeText(MainActivity.this,"Student is absent",Toast.LENGTH_LONG).show();
                    }
                    else if (radioButton_leave.isChecked())
                    {
                        Toast.makeText(MainActivity.this,"Student is leave",Toast.LENGTH_LONG).show();
                    }
                }
            });


        }