添加 RadioButton 后 ListView onClickListener() 不起作用 [英] ListView onClickListener() does not work after adding RadioButton

查看:23
本文介绍了添加 RadioButton 后 ListView onClickListener() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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):

The layout for each list item is (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 作为列表:

In Java code, I use SimpleAdapter for the list:

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 则有效)

As you see, in above code, in the list item layout, I added a RadioButton, after I added this button, my list onClickListener does not work anymore, why?? (It works if it's without RadioButton on list item layout)

推荐答案

为您的 RadioButton 设置以下属性:

Set the following properties to your RadioButton:

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

在您的 OnItemClickListener 中,您需要通过代码设置单选按钮的 checked 标志.

and in your OnItemClickListener, you need to set the radio button's checked flag by code.

如下设置你的 ListView:

Set you ListView as below:

<ListView
  android:id="@+id/my_list"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

这篇关于添加 RadioButton 后 ListView onClickListener() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆