ListView项链接使用列表项内的按钮单击行的状态

时间:2014-02-13 13:49:05

标签: android listview android-listview android-arrayadapter

如何将第1行列表项目中的“点击次数”与行#2中的单选按钮进行链接。

目前它是:点击该行将显示“焦点”hilight但不会触发单选按钮选择,单击RB将“检查”radiobutton但不会产生“hilight”效果。

但是我想让它像一个一样:“点击行将显示焦点并在RB上进行选择并点击RB将检查radiobutton和hilight行”。

有没有办法实现这个目标?有了这个设置,我想我可以改变行,所以它是一个不包含radiobutton的radiobutton。

我有一个自定义ArrayAdapter来处理radiobutton点击(以及处理无线电按钮的“组”)。

注射#1

list item selected

注射#2

list item radiobutton selected

列表视图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

<?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>

我暂时做的是: 我将RadioButton的clickable设置为false。 在ArrayAdapter中,而不是RB我在行视图上设置clickListener(基于视图类型)。

4 个答案:

答案 0 :(得分:3)

实现此目的的最佳方法是在row.xml中使用CheckedTextView。默认情况下,android提供你在布局android.R.layout.simple_list_item_single_choice中提出的行为。但单选按钮将正确对齐。如果您希望单选按钮左对齐,则必须自定义simple_list_item_single_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="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
/>

到这个

<?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重新定位到左侧,方法是将其绘制在左侧而不是默认右侧。同样,您可以针对相同的行为调整多个Choice项目。

答案 1 :(得分:1)

为什么不以编程方式检查OnItemClicklistener的{​​{1}}中的RadioButton,反之亦然?

例如,

ListView

然后,同样在您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; } }); setSelected(true) View RadioButton OnCheckedChangeListener onCheckedChanged内的RadioGroup上调用{{1}}。

答案 2 :(得分:1)

您应该使用行作为单选按钮,而不是自己创建自定义单选元素。然后事件将直接传递给单选按钮。

详情请参阅:http://rudisss.blogspot.in/2012/10/listview-with-radio-buttons-and.html

答案 3 :(得分:1)

截至目前的设计,我们可以使用RadioButton简单地实现此要求。为实现此目的,只需在行布局中移除Radiobutton外的RelativeLayout,并将RadioButon作为行布局的父级。并为RadioButton编写一个自定义选择器。它会解决你的问题。

相关问题