ListView的onClickListener()将单选按钮后无法正常工作 [英] ListView onClickListener() does not work after adding RadioButton

查看:160
本文介绍了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 ):

<?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的code,我用 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+""); 
        }
    });

正如你看到的,在上述code,列表中的项目布局,我添加了一个单选,之后我加入这个按钮,我的列表 onClickListener 不工作了,为什么? (它的工作原理,如果它没有单选列表项的布局)

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)

推荐答案

设置以下属性,以您的单选按钮

Set the following properties to your RadioButton:

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

和你的 OnItemClickListener 的,你需要设置单选按钮的检查标志由code。

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" />

这篇关于ListView的onClickListener()将单选按钮后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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