在ListView的RadioGroup内添加RadioButton

时间:2018-08-30 22:12:26

标签: java android listview android-radiogroup

我一直试图在列表视图的单选组内添加单选按钮。 因此,如果我单击一个单选按钮,它将对其进行检查,但是如果单击另一个,则不会删除先前的选择。我是android的初学者,所以如果我做错了什么,请原谅我。

这是我的主要活动,并且广播组不在列表中

type NormalizeOne<T> =
  [T] extends [number] ? number :
  [T] extends [number | undefined] ? number | undefined :
  [T] extends [string] ? string :
  [T] extends [string | undefined] ? string | undefined :
  [T] extends [number[]] ? number[] :
  [T] extends [number[] | undefined] ? number[] | undefined :
  [T] extends [string[]] ? string[] :
  [T] extends [string[] | undefined] ? string[] | undefined :
  [T] extends [Function] ? never :
  [T] extends [Array<Object>] ? number[] :
  [T] extends [Array<Object> | undefined] ? number[] | undefined :
  [T] extends [Object] ? number :
  T;  // not reached due to compiler issue

这是我对列表中每个项目的自定义视图

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="20dp">

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/car_list_radio_group">

                <ListView
                    android:id="@+id/vehicle_list"
                    android:clickable="true"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"></ListView>

            </RadioGroup>

        </LinearLayout>

这是我的列表适配器

<?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="match_parent"
    android:alwaysDrawnWithCache="true"
    android:backgroundTint="#ffffff">


    <RadioButton
        android:id="@+id/car_selected"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:buttonTint="@color/tesla_red"
        android:padding="10dp"
        android:text=""
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/vin"
        android:layout_width="183dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/car_selected"
        android:layout_marginStart="30dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="@android:color/white"
        android:textSize="12sp" />

</RelativeLayout>

如果您需要看别的东西,请告诉我。

2 个答案:

答案 0 :(得分:0)

RadioGroupLinearLayout,并且有RadioButton个子代才能正确处理检查/取消检查。您将必须删除它,然后自己从适配器处理检查/取消检查逻辑。

答案 1 :(得分:0)

将此添加到您的list_item.xml中 您将在每一行中获得radioGroup 然后从main_activity.xml中删除RadioGroup

希望这对您有帮助...

<?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="match_parent"
android:alwaysDrawnWithCache="true"
android:backgroundTint="#ffffff">

<RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/car_list_radio_group">
  <RadioButton
    android:id="@+id/car_selected"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:buttonTint="@color/tesla_red"
    android:padding="10dp"
    android:text=""
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:textStyle="bold" />

    <RadioButton
    android:id="@+id/car_not_selected"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_torightof="@+id/car_selected" 
    android:buttonTint="@color/tesla_red"
    android:padding="10dp"
    android:text=""
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:textStyle="bold"
</RadioGroup>
 <TextView
    android:id="@+id/vin"
    android:layout_width="183dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/car_selected"
    android:layout_marginStart="30dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:textColor="@android:color/white"
    android:textSize="12sp" />

 </RelativeLayout>
相关问题