添加RadioButton后,ListView onClickListener()不起作用

Lee*_*fin 12 android android-widget android-emulator android-intent android-layout

我有一个ListView(my_list.xml):

 <ListView
        android:id="@+id/my_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
      />
Run Code Online (Sandbox Code Playgroud)

每个列表项的布局是(list_item.xml):

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

    <ImageView 
          android:id = "@+id/my_icon" 
          android:layout_width ="wrap_content" 
          android:layout_height ="wrap_content"
          android:layout_centerVertical="true"  
     /> 
    <TextView 
         android:id="@+id/my_str" 
         android:layout_width="wrap_content" 
         android:layout_height = "wrap_content" 
         android:layout_toRightOf="@id/my_icon"
     /> 

     <!--This radio button makes the list item unselectable, why?-->
     <RadioButton 
         android:id="@+id/my_radio_btn"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerVertical="true"
         android:layout_alignParentRight="true"
         />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在Java代码中,我SimpleAdapter用于列表:

my_list = (ListView) findViewById(R.id.my_list);

SimpleAdapter adapter = new SimpleAdapter(context, getOptions(),
           R.layout.list_item, 
           new String[] { "icon1","str1" }, 
           new int[] {R.id.my_icon, R.id.my_str });

my_list.setAdapter(adapter);

//onClickListener does not work after I added RadioButton in list item layout
my_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            Log.v("SELECTED", position+""); 
        }
    });
Run Code Online (Sandbox Code Playgroud)

如您所见,在上面的代码中,在列表项布局中,我添加了一个RadioButton,在我添加此按钮后,我的列表onClickListener不再有效,为什么?(如果没有RadioButton列表项目布局,它可以工作)

waq*_*lam 24

将以下属性设置为RadioButton:

android:focusable="false"
android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

OnItemClickListener中,您需要按代码设置单选按钮的已检查标志.


将ListView设置如下:

<ListView
  android:id="@+id/my_list"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)