ListView onClickListener () не работает после добавления RadioButton - PullRequest
12 голосов
/ 27 марта 2012

У меня есть ListView ( my_list.xml ):

 <ListView
        android:id="@+id/my_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
      />

Макет для каждого элемента списка ( 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>

В коде 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+""); 
        }
    });

Как вы видите, в приведенном выше коде в макет элемента списка я добавил RadioButton, после того как я добавил эту кнопку, мой список onClickListener больше не работает, почему ??(Работает, если на макете элемента списка нет RadioButton)

Ответы [ 2 ]

24 голосов
/ 27 марта 2012

Установите следующие свойства для RadioButton :

android:focusable="false"
android:focusableInTouchMode="false"

и в вашем OnItemClickListener вам необходимо установить переключатель флажок помечать кодом.


Установить ListView, как показано ниже:

<ListView
  android:id="@+id/my_list"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />
2 голосов
/ 27 марта 2012

добавьте этот код в XML-код RadioButton:

android:focusable="false"
android:focusableInTouchMode="false"

Еще одно ключевое слово для решения этой проблемы - TouchDelegate.

Редактировать:

https://stackoverflow.com/a/5528945/1285331

...