2011-10-18 162 views
3

我有一个列表视图动态填充CheckedTextViews。列表视图被设置为多选模式。我使用OnItemClickListener来响应我的列表视图上的点击。此外,我用CheckedTextView的布局制作了一个XML文件(实际上它只是一个标准的android.R.layout.simple_list_item_multiple_choice的副本)。所以在这种情况下,所有工作都很好:当我点击列表视图中的某个项目时,相应的checkedtextview就会被检查。但是当我尝试使用以下布局时,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,他们不从列表视图的列表项接收click事件。

回答

0

那么即使他们没有收到OnClickEvent,您仍然可以使用相同大小的List Items的布尔ArrayList维护这些复选框的状态。 Here,我刚刚回答了这样的问题。我希望它会给你一个更好的主意。

1

我猜不同。如果您查看source code of simple_list_item_multiple_choice.xml,则会看到android:checkMark属性设置为?android:attr/listChoiceIndicatorMultiple。这是CheckedTextView内部用来绘制该复选框的状态,正如您可能从its source code中看到的那样。

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

我错了,你猜对了。看起来,自定义ListView的行需要实现Checkable,其中CheckedTextView确实,但LinearLayout没有。有关详细信息,请参阅另一个StackOverflow线程中的this answer

0

我与同样的问题,所以我改变了布局到android.R.layout.simple_list_item_multiple_choice,而不是使用自定义布局与CheckedTextView。现在它正在运行!

0

有此问题的两种简单的解决方案:

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

  2. 如果您的minSDK是11以上,请自定义android:checkMarke并将状态设置为Activated。下面是一个例子:

示例代码:

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

相关的问题:What is the difference between the states selected, checked and activated in Android?

0

此问题是由ATTR singleLine = true引起的。如果你删除这个或用maxLines = 1替换,它会正常工作。其实,当你点击该项目—时,状态会被检查。我认为这是一个Android错误。