Android ListView项目链接单击列表项目内带有按钮的行的状态

Android ListView项目链接单击列表项目内带有按钮的行的状态,android,listview,android-listview,android-arrayadapter,Android,Listview,Android Listview,Android Arrayadapter,如何将列表项行快照1上的“单击”链接到行快照2内的单击radiobutton上 目前情况是:单击行将显示“聚焦”hilight,但不会触发单选按钮选择,单击RB将“检查”单选按钮,但不会产生“hilight”效果 但我想让它作为一个整体工作:“单击行将显示焦点,并在RB上进行选择,单击RB将选中radiobutton和hilight行” 有没有办法做到这一点?有了这个设置,我想我可以更改行,使它成为一个radiobutton而不包含radiobutton 我有一个定制的ArrayAdapter来

如何将列表项行快照1上的“单击”链接到行快照2内的单击radiobutton上

目前情况是:单击行将显示“聚焦”hilight,但不会触发单选按钮选择,单击RB将“检查”单选按钮,但不会产生“hilight”效果

但我想让它作为一个整体工作:“单击行将显示焦点,并在RB上进行选择,单击RB将选中radiobutton和hilight行”

有没有办法做到这一点?有了这个设置,我想我可以更改行,使它成为一个radiobutton而不包含radiobutton

我有一个定制的ArrayAdapter来处理单选按钮的点击(以及处理单选按钮的“组”)

shot#1

shot#2

列表视图xml

<ListView
    android:id="@+id/lv_radiolist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:divider="@null"
    android:dividerHeight="0dp"
    tools:listitem="@layout/radiobutton_list_item" >
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="@dimen/filterlist_sub_header_pad_left"
android:paddingRight="@dimen/filterlist_sub_header_pad_right" >

<RadioButton
    android:id="@+id/rb_option"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:checked="true"
    android:focusable="false"
    android:text="Not marked favourite" />
</RelativeLayout>

行xml

<ListView
    android:id="@+id/lv_radiolist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:divider="@null"
    android:dividerHeight="0dp"
    tools:listitem="@layout/radiobutton_list_item" >
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="@dimen/filterlist_sub_header_pad_left"
android:paddingRight="@dimen/filterlist_sub_header_pad_right" >

<RadioButton
    android:id="@+id/rb_option"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:checked="true"
    android:focusable="false"
    android:text="Not marked favourite" />
</RelativeLayout>

我暂时做的是: 我将单选按钮的clickable设置为false。
在ArrayAdapter中,我在RB上设置了行视图上的clickListener(基于视图类型)。

为什么不简单地以编程方式检查
列表视图的
OnItemClicklistener
中的单选按钮,反之亦然

那么比如说,

listView.setOnItemClickListener(new OnItemClickListener() {

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

              // set the RadioButton of the corresponding
             // view that was clicked by invoking setChecked(true) 
            // on that RadioButton;

        }
});
listView.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//设置相应按钮的单选按钮
//通过调用setChecked单击的视图(true)
//在那个单选按钮上;
}
});

然后,在
视图
上调用
setSelected(true)
,该视图包含
OnCheckedChangeListener
中的
单选按钮
,在
onCheckedChanged
实现的
放射组
中,您应该使用一行作为单选按钮,而不是自己创建自定义的无线电元素。然后事件将直接传递到单选按钮


有关详细信息,请参阅:

完成此操作的最佳方法是在row.xml中使用CheckedTextView。默认情况下,android提供您在布局android.R.layout.simple\u list\u item\u single\u choice中要求的行为。但是单选按钮将右对齐。如果您希望单选按钮左对齐,则必须进行自定义,即


对此

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:textAppearance="?android:attr/textAppearanceLarge" />


只需将listChoiceIndicator重新定位到左侧,方法是将其绘制在左侧而不是默认的右侧。类似地,您可以为相同的行为调整多项选择项。

根据您当前的设计,我们只需使用单选按钮即可实现此要求。为了实现这一点,只需删除行布局中Radiobutton外部的RelativeLayout,并将Radiobutton作为行布局的父级。并为RadioButton编写自定义选择器。这会解决您的问题。

您知道其他路线吗?有了这一点,我必须用这一行布局在每个ListView上进行单击,并且需要知道如何调用选择方法。此外,我还必须添加代码,以便从radiobutton的click listener对背景项进行高亮显示。我想要一些自给自足的东西。我不知道还能怎么做,对不起-我以前从来没有做过这样的事情,所以我不知道什么是最好的方法。我意识到这可能是一个需要实施的长期解决方案;希望有人能找到更好的。对不起,我不清楚你想做什么。你能再解释一下吗?RB代表什么?关于列表的两个屏幕截图,它们是相同的还是两个单独的列表?两个列表中的行之间需要什么关系。RB-是R adio B按钮的缩写。2个屏幕截图显示相同的列表(包含不同类型的行)。并显示在RB行的不同部分单击的结果。第一个显示亮起的行但未选中的RB,第二个显示选中的RB但未选中的行。将代码放在这里,以便用户可以参考它,即使链接不可用。我已经提到,这不是一个解决方案,即,我可以在其中包含多个单选按钮。或者我的设计需要这个额外的“容器”,例如用于RB上应用的边距。是否有RadioButton自定义选择器的示例?将RadioButton设置为行不是一个解决方案,因为我无法正确应用边距(它不会移动RadioButton图标,但会增加rb图标和文本之间的空间),如果您对自定义RadioButton感兴趣,可以这样做。