我有一个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,它可以工作)
解决方法
将以下属性设置为您的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" />
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。