4

我有一个用 CheckedTextViews 动态填充的列表视图。列表视图设置为多选模式。我正在使用 OnItemClickListener 来响应对我的列表视图的点击。我还用 CheckedTextView 的布局制作了一个 XML 文件(实际上它只是标准 android.R.layout.simple_list_item_multiple_choice 的副本)。所以在这种情况下一切正常:当我单击列表视图中的一个项目时,相应的checkedtextview 会被选中。但是当我尝试使用以下布局时,checkedtextview 不会响应单击并且仍然未选中。

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"  
    android:orientation="vertical"  
    android:padding="5px">
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:drawableLeft="@drawable/checkbox_selector"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:background="#00ffffff"
        android:textSize="15sp"
        android:singleLine="true"/>
</LinearLayout>    

我猜这是因为 CheckedTextViews 被放入了 LinearLayout 并且它们没有从 Listview 的列表项中接收到单击事件。

4

5 回答 5

1

估计不一样 如果您查看 的源代码simple_list_item_multiple_choice.xml,您会看到一个android:checkMark属性设置为?android:attr/listChoiceIndicatorMultiple. 这就是CheckedTextView内部用来绘制处于任何状态的复选框的内容,正如您可以从其源代码中看到的那样。

但是CheckedTextView您的布局中的定义缺少此属性。这我会责怪,而不是CheckedTextView在一个LinearLayout元素中使用。

我错了,你猜对了。似乎自定义的行ListView需要实现CheckableCheckedTextViewLinearLayout没有。有关详细信息,请参阅另一个 StackOverflow 线程中的此答案

于 2011-10-28T14:42:06.107 回答
1

我遇到了同样的问题,所以我将布局更改为 android.R.layout.simple_list_item_multiple_choice,而不是使用带有 CheckedTextView 的自定义布局。

于 2013-07-26T19:10:48.200 回答
0

这个问题有两个简单的解决方案:

  1. 删除 LinearLayout 和仅用户 CheckedTextView (是的,这是可能的)。所以布局的主要元素是可检查的,它会被标记。

  2. 如果您的 minSDK 是上述 11,请自定义 android:checkMarke 并将状态设置为已激活。这是一个例子:

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>  
   <item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />  
   <item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />  
   <item android:state_checked="true" android:drawable="@drawable/checkbox_selected" /> 
</selector>

来源:http ://www.jiahaoliuliu.com/2013/08/customizing-checkbox-in-android.html

中层解决方案是自定义列表视图的onItemClickListener,如下代码所示: https ://github.com/jiahaoliuliu/CustomizedListRow

困难但正确的解决方案是 MarvinLab 提出的解决方案:http: //www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

相关问题:Android 中选择、选中和激活的状态有什么区别?

于 2013-09-04T14:24:20.740 回答
0

好吧,即使他们没有收到 OnClickEvent,您也可以使用列表项大小相同的布尔 ArrayList 来维护这些复选框的状态。在这里,我刚刚回答了类似的问题。我希望它会给你一个更好的主意。

于 2011-10-19T10:54:57.963 回答
0

此问题是由 attr 引起的singleLine = true。如果您删除它或替换为maxLines = 1,它将正常工作。实际上,当您单击该项目时会检查状态 - 我认为这是一个 Android 错误。

于 2017-03-31T01:15:44.537 回答